1

I am trying to download a gzip file using curl and php and saving it in a specified location. Below is the code that I am using but everytime it's saving an empty file allthough the gzip file that it's downloading is proper

$file_zip = 'abc.gz'
$fp = fopen("$file_zip", "w"); 
$request = curl_init();
$headers = array('Content-type: application/x-gzip','Connection: Close');
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_TIMEOUT, 0);
curl_setopt($request, CURLOPT_CONNECTTIMEOUT,0);
curl_setopt($request, CURLOPT_HTTPHEADER,$headers);
curl_setopt($request, CURLOPT_FAILONERROR, true);
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($request, CURLOPT_AUTOREFERER, true);
curl_setopt($request, CURLOPT_BINARYTRANSFER,true);

curl_setopt($request, CURLOPT_FILE, $fp);

$zipFile = curl_exec($request);
6
  • did you use copy()? is there any special reason to use cURL?
    – Taha Paksu
    Commented May 9, 2012 at 18:44
  • actually , the actual file gets generated at some temporary location from my main controller(the url of which is given as the curl url) and this particular php script picks it up and eventually uploads it to an external ftp server.
    – deGee
    Commented May 9, 2012 at 18:48
  • Ok, what if you don't use any headers? like $headers = 0; ?
    – Taha Paksu
    Commented May 9, 2012 at 18:53
  • returntransfer and file are kinda contradictory. "return the requested url to the php script", but also "no, write it out to a file". Check if $zipfile contains the actual zip data (e.g. echo strlen($zipFile).
    – Marc B
    Commented May 9, 2012 at 19:37
  • I don't know, I didn't try but sending the "Connection-Close" header might be closing the connection before getting the result, and that might lead to an empty file.
    – Taha Paksu
    Commented May 9, 2012 at 20:03

1 Answer 1

3

I found a method that downloads a file to a location with cUrl. Hope it helps:

<?php 
function get_file1($file, $local_path, $newfilename) 
{ 
    $err_msg = ''; 
    echo "<br>Attempting message download for $file<br>"; 
    $out = fopen($newfilename, 'wb'); 
    if ($out == FALSE){ 
      print "File not opened<br>"; 
      exit; 
    } 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_FILE, $out); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_URL, $file); 

    curl_exec($ch); 
    echo "<br>Error is : ".curl_error ( $ch); 

    curl_close($ch); 
    //fclose($handle); 

}//end function 
?>

The source is : http://www.weberdev.com/get_example.php3?ExampleID=4009

4
  • Thanks @tpaksu , but my curl call and the backend file creation operates a bit differently . Actually , the curl call to the controller generates 3 separate reports (in csv format) , and then these reports are gzipped as a tar ball to a temporary file and then streamed out , as a response to my curl call , using passthru() function . Once , the file is streamed out , the temporary gzipped file is deleted.
    – deGee
    Commented May 9, 2012 at 20:34
  • Ok but here's the thing, the backend will create the gzip and save to a temporary location as file, as the curl is called, after finishing the file creation, return a string from the backend and curl will end at this time returning the outputted string, then you'll check the string and copy the file to your location via copy(). curl will only tell the backend to create the file and return some string after the creation completes.
    – Taha Paksu
    Commented May 9, 2012 at 20:46
  • But whatif my gzip file is created on a different server alltogether , than the server where my curl script is running, Will copy() work for remote files as well?
    – deGee
    Commented May 9, 2012 at 20:49
  • after the file creation succeeds it'll return the string and curl will return at that time.
    – Taha Paksu
    Commented May 9, 2012 at 21:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.