How to save a remote image to local disk

$image_url = “”;
$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);

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

// 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.


This site uses Akismet to reduce spam. Learn how your comment data is processed.