1

How do I download an external image off a url with cURL?

3
  • stackoverflow.com/questions/1898711/… Commented Nov 29, 2010 at 16:06
  • Any reason why would you choose cURL over file_get_contents() ? Commented Nov 29, 2010 at 16:06
  • I am not sure that I will have fopen enabled on my server, so I am avoiding using file_get_contents(). Commented Nov 29, 2010 at 17:53

2 Answers 2

3

See here:

http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/

function save_image($img,$fullpath){
 $ch = curl_init ($img);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
 $rawdata=curl_exec($ch);
 curl_close ($ch);
 if(file_exists($fullpath)){
  unlink($fullpath);
 }
 $fp = fopen($fullpath,'x');
 fwrite($fp, $rawdata);
 fclose($fp);
}

Other articles/sources:

http://forums.digitalpoint.com/showthread.php?t=371632

http://www.bitrepository.com/download-image.html

http://php.bigresource.com/Track/php-Jjg3DsKY/

Sign up to request clarification or add additional context in comments.

Comments

0

from php.net - also reads into a string.

   <?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "example.com");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);     
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.