使用php curl下载并保存gzip文件 [英] download and save a gzip file using php curl

查看:637
本文介绍了使用php curl下载并保存gzip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用curl和php下载gzip文件并将其保存在指定位置。下面是我使用的代码,但每次它保存一个空文件,尽管它下载的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);


推荐答案

我发现了一种将文件下载到位置的方法与cUrl。希望它有帮助:

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

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

这篇关于使用php curl下载并保存gzip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆