Fetch remote images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cbcb
Forum Newbie
Posts: 2
Joined: Sun Jan 16, 2005 4:37 pm

Fetch remote images

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
cbcb
Forum Newbie
Posts: 2
Joined: Sun Jan 16, 2005 4:37 pm

Post by cbcb »

Unfortunatly, it seems I do not have that function available.
If it's any help, the server is running 4.1.1
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
Post Reply