1

我正在尝试使用 curl 和 php 下载 gzip 文件并将其保存在指定位置。下面是我正在使用的代码,但每次它都保存一个空文件,尽管它下载的 gzip 文件是正确的

$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);
4

1 回答 1

3

我找到了一种使用 cUrl 将文件下载到某个位置的方法。希望能帮助到你:

<?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 
?>

来源是:http ://www.weberdev.com/get_example.php3?ExampleID=4009

于 2012-05-09T19:32:45.960 回答