Tuesday, September 03, 2013

Downloading a remote file using curl

Just like the contents of a remote url can be fetched, a remote file with a given url can be downloaded and saved to local storage too.

    /**
        Download remote file in php using curl
        Files larger that php memory will result in corrupted data
    */
     $url  = 'http://localhost/sugar.zip';
     $path = '/var/www/lemon.zip';

     $ch = curl_init($url);
     if($ch === false)
     {
         die('Failed to create curl handle');
     }

     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

     $data = curl_exec($ch);
     file_put_contents($path, $data);
     echo 'File download complete';

     curl_close($ch); 

The above program can download remote files but has few restrictions. If the download file size is larger than the total amount of memory available to php, then either a memory exceeded error would be thrown or the downloaded file would be corrupt.

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 60527991 bytes) in  /var/www/curl.php on line 19

Hence the problem has to be fixed as shown in the next code example  


/**
    Download remote file in php using curl
    chunking with fopen
*/
$url  = 'http://localhost/bang.zip';
$path = '/var/www/lemon.zip';

$ch = curl_init($url);
if($ch === false)
{
    die('Failed to create curl handle');
}

$fp = fopen($path, 'w');
  
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
  
$data = curl_exec($ch);
  
curl_close($ch);
fclose($fp);

The above code would download large files and save them without any problem. The option CURLOPT_FILE tells curl to write the output to a file.

No comments:

Post a Comment