Download external images with cURL

software development

#1

$ch = curl_init(“http://www.site.com/image.jpg”);
$fp = fopen(“images/my_downloaded_image.jpg”, “w”);

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);


I’ve seen alot of people wondering how to download images
from external servers using cURL, (and so was I).
I was’nt really expecting to see this simple code work at first
try, but it actually did. :-).

Another version, using fopen()/fread()/fwrite():

if($handle = fopen(‘http://www.site.com/image.jpg’, ‘rb’)) {
$newfile_name = “images/image.jpg”;
$newfile = fopen($newfile_name,‘wb’);
while (!feof($handle)) {
$chunk = fread($handle,1024);
fwrite($newfile,$chunk);
}
fclose($newfile);
fclose($handle);
echo ‘Great Success!’;
} else {
echo ‘Could’nt find image.jpg!’;
}


Hope you’ll find it useful!