doutu1939 2012-05-09 18:40
浏览 51
已采纳

使用php curl下载并保存gzip文件

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);
  • 写回答

1条回答 默认 最新

  • douwa0280 2012-05-09 19:32
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: