Page 1 of 1

Fetch remote images

Posted: Sun Jan 16, 2005 4:38 pm
by cbcb
Hi,
I am trying to come up with a snippet that will fetch an image off a remote server, and copy it to a local folder. The format it will most often be in is: http://somewhere.com:8080/pic_1.jpg

Should I use something like fsockopen?

Thanks,
Cbcb

Posted: Sun Jan 16, 2005 7:35 pm
by feyd
if file URL wrappers are on at your server, which they likely are, you can use file_get_contents() or any of the other file functions to get the file.

fsockopen() is a bit more complicated to fetch a remote file with, unless you don't have file URL wrappers enabled..

Posted: Sun Jan 16, 2005 8:31 pm
by cbcb
Unfortunatly, it seems I do not have that function available.
If it's any help, the server is running 4.1.1

Posted: Sun Jan 16, 2005 8:38 pm
by feyd
wow.. really old version.. you should be able to use the fopen()/fread()/fclose() combination then.

example:

Code: Select all

$fp = fopen($url);
if(!$fp)
  die('unabled to load file');

$buf = '';
while(!feof($fp))
{
  $buf .= fread($fp, 1024);
}
fclose($fp);

echo $buf;