If you have PHP5 and the HTTP stream wrapper enabled on your server, it's incredibly simple to copy it to a local file:
copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');
This will take care of any pipelining etc. that's needed. If you need to provide some HTTP parameters there is a third 'stream context' parameter you can provide.
/*******************************************************************************************************************************************************/
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);
/*******************************************************************************************************************************************************/
$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/" . basename($src);
file_put_contents($dest, file_get_contents($src));
You need to specify the filename. I added basename($src)
which will write to the same filename that the original was. Be careful if you're copying from other directories, basename()
only returns the filename so if you copy /image.jpg and /a/image.jpg you'll write over the original.