Forum Moderators: coopster

Message Too Old, No Replies

How to cache a remote image?

         

cantona

4:22 pm on Apr 6, 2009 (gmt 0)

10+ Year Member



Hi, I'd like to hit an image URL and cache it on my server.

Is this the best practice?


$image = 'http://www.domain.com/image.jpg';
$remote_image = fopen($image, "rb");
$image_data = fread($remote_image, filesize($image));
$fp = fopen('/tmp/new_file_'.$#*$!x, 'w');
fwrite($fp, $image_data);
fclose($remote_image);
fclose($fp);

coopster

8:13 pm on Apr 7, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Whatever works best for you :)
file_get_contents [php.net] would be much less code and is binary-safe.