I need to save an image from a url using CURL and save it to a folder on my server. I've been battling with this code to no avail. Ideally I'd like to grab the image and save it as "photo1" or something. Help!
function GetImageFromUrl($link) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch,CURLOPT_URL,$link); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result=curl_exec($ch); curl_close($ch); return $result; } $sourcecode = GetImageFromUrl($iticon); $savefile = fopen(' /img/uploads/' . $iconfilename, 'w'); fwrite($savefile, $sourcecode); fclose($savefile);
try this:
function grab_image($url,$saveto){ $ch = curl_init ($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $raw=curl_exec($ch); curl_close ($ch); if(file_exists($saveto)){ unlink($saveto); } $fp = fopen($saveto,'x'); fwrite($fp, $raw); fclose($fp); }
and ensure that in php.ini allow_url_fopen is enable
Option #1
Instead of picking the binary/raw data into a variable and then writing, you can use CURLOPT_FILE
option to directly show a file to the curl for the downloading.
Here is the function:
// takes URL of image and Path for the image as parameter function download_image1($image_url, $image_file){ $fp = fopen ($image_file, 'w+'); // open file handle $ch = curl_init($image_url); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want curl_setopt($ch, CURLOPT_FILE, $fp); // output to file curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1000); // some large value to allow curl to run for a long time curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); // curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints curl_exec($ch); curl_close($ch); // closing curl handle fclose($fp); // closing file handle }
And here is how you should call it:
// test the download function download_image1("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg");
Option #2
Now, If you want to download a very large file, that case above function may not become handy. You can use the below function this time for handling a big file. Also, you can print progress(in %
or in any other format) if you want. Below function is implemented using a callback
function that writes a chunk of data in to the file in to the progress of downloading.
// takes URL of image and Path for the image as parameter function download_image2($image_url){ $ch = curl_init($image_url); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1000); // some large value to allow curl to run for a long time curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); curl_setopt($ch, CURLOPT_WRITEFUNCTION, "curl_callback"); // curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints curl_exec($ch); curl_close($ch); // closing curl handle } /** callback function for curl */ function curl_callback($ch, $bytes){ global $fp; $len = fwrite($fp, $bytes); // if you want, you can use any progress printing here return $len; }
And here is how to call this function:
// test the download function $image_file = "local_image2.jpg"; $fp = fopen ($image_file, 'w+'); // open file handle download_image2("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2"); fclose($fp); // closing file handle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With