Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving image from PHP URL

Tags:

php

image

I need to save an image from a PHP URL to my PC. Let's say I have a page, http://example.com/image.php, holding a single "flower" image, nothing else. How can I save this image from the URL with a new name (using PHP)?

like image 670
riad Avatar asked Apr 07 '09 06:04

riad


People also ask

How do I download an image from a URL?

Click on the Download Image from URL button, the field will appear on the right. Enter the full web address of the image. Click on the arrow to the right of the field and select the Force Check checkbox. Then click the Save button.

Can I download a PHP file from URL?

If the server is configured correctly, you cannot download a PHP file. It will be executed when called via the webserver. The only way to see what it does is to gain access to the server via SSH or FTP or some other method.

How do I copy an image from one directory to another in PHP?

The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten.


2 Answers

If you have allow_url_fopen set to true:

$url = 'http://example.com/image.php'; $img = '/my/folder/flower.gif'; file_put_contents($img, file_get_contents($url)); 

Else use cURL:

$ch = curl_init('http://example.com/image.php'); $fp = fopen('/my/folder/flower.gif', 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); 
like image 62
vartec Avatar answered Oct 06 '22 08:10

vartec


Use PHP's function copy():

copy('http://example.com/image.php', 'local/folder/flower.jpg'); 

Note: this requires allow_url_fopen

like image 23
Halil Özgür Avatar answered Oct 06 '22 07:10

Halil Özgür