Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Image from Remote Server Over HTTP

I am looking for a simple way to import/copy images from remote server to a local folder using PHP. I have no FTP access to the server, but all remote images can be accessed via HTTP (i.e. http://www.mydomain.com/myimage.jpg).

Example use: A user wishes to add an image to his profile. The image already exists on the web and the user provides with a direct URL. I do not wish to hotlink the image but to import and serve from my domain.

like image 581
GiladG Avatar asked May 26 '09 07:05

GiladG


2 Answers

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.

like image 73
Ciaran McNulty Avatar answered Nov 16 '22 10:11

Ciaran McNulty


if nothing works, use this quick solution

$imageString = file_get_contents("http://example.com/image.jpg");
$save = file_put_contents('Image/saveto/image.jpg',$imageString);
like image 32
dazzafact Avatar answered Nov 16 '22 11:11

dazzafact