How to save a remote image to local disk

$image_url = “http://example.com/image.jpg”;
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);

// write to local file
$f = fopen(‘/home/www/path/image.jpg’, ‘w’);
fwrite($f, $image);
fclose($f);

// output to browser
header(“Content-type: image/jpeg”);

print $image;

?>

Be the first to comment

Leave a Reply

Your email address will not be published.


*