views:

3607

answers:

8

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.

A: 

Use a GET request to download the image and save it to a web accessible directory on your server.

As you are using PHP, you can use curl to download files from the other server.

Peter Stuifzand
A: 

Assuming you're using .NET, which you may not be:

WebClient wc = new WebClient();
byte[] data = wc.DownloadData(path);
MemoryStream ms = new MemoryStream(data);
return (Bitmap)Bitmap.FromStream(ms);
joshcomley
The tag says he's using PHP...
WebDevHobo
Ah you're right hadn't spotted that. It's probably useful for other people when they Google search though, the question is very specific.
joshcomley
+1  A: 

It's extremely simple using file_get_contents. Just provide the url as the first parameter.

PatrikAkerstrand
This is locked down to local filesystems on a lot of shared hosts for security reasons. cURL is better, faster, stronger, etc... And usually available on those hosts that block fopen on remote filesystems.
Oli
if cURL is available, that would be even better. see <a href="http://se2.php.net/curl">cUrl at php.net</a>
PatrikAkerstrand
+2  A: 

You've got about these four possibilities:

  • Remote files. This needs allow_url_fopen to be enabled in php.ini, but it's the easiest method.

  • Alternatively you could use cURL if your PHP installation supports it. There's even an example.

  • And if you really want to do it manually use the HTTP module.

  • Don't even try to use sockets directly.

Georg
A: 

Here's the most basic way:

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";

$rfile = fopen($url, "r");
$lfile = fopen($dir . basename($url), "w");

while(!feof($url)) fwrite($lfile, fread($rfile, 1), 1);

fclose($rfile);
fclose($lfile);

But if you're doing lots and lots of this (or your host blocks file access to remote systems), consider using CURL, which is more efficient, mildly faster and available on more shared hosts.

You can also spoof the user agent to look like a desktop rather than a bot!

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";
$lfile = fopen($dir . basename($url), "w");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FILE, $lfile);

fclose($lfile);
curl_close($ch);

With both instances, you might want to pass it through GD to make sure it really is an image.

Oli
For checking the image with GD, consider running this right after you save the file: http://www.psoug.org/snippet/Check_if_an_image_exists_GD_364.htm - if it comes back false, delete the file immediately.
Oli
A: 

Since you've tagged your question 'php', I'll assume your running php on your server. Your best bet is if you control your own web server, then compile cURL into php. This will allow your web server to make requests to other web servers. This can be quite dangerous from a security point of view, so most basic web hosting providers won't have this option enabled.

Here's the php man page on using cURL. In the comments you can find an example which downloads and image file.

If you don't want to use libcurl, you could code something up using fsockopen. This is built into php (but may be disabled on your host), and can directly read and write to sockets. See Examples on the fsockopen man page.

brianegge
+1  A: 
+8  A: 

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.

Ciaran McNulty
Remember allow_url_fopen has to be = on in php.ini.
mauro.dec