Save image from url with curl PHP Save image from url with curl PHP curl curl

Save image from url with curl PHP


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 parameterfunction 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 functiondownload_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 parameterfunction 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 handledownload_image2("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2");fclose($fp);                                  // closing file handle


If you want to download an image from https:

$output_filename = 'output.png';$host = "https://.../source.png"; // <-- Source image url (FIX THIS)$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $host);curl_setopt($ch, CURLOPT_VERBOSE, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_AUTOREFERER, false);curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // <-- don't forget thiscurl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // <-- and this$result = curl_exec($ch);curl_close($ch);$fp = fopen($output_filename, 'wb');fwrite($fp, $result);fclose($fp);