下载zip PHP

时间:2013-09-24 06:40:54

标签: php curl

我试图获取一个zip文件(之前我不知道大小和名称),请求xml返回一个zip文件。我想下载它,但有时下载全部(约16mb)有时不下载(2mb或4mb或1mb)我不知道为什么。

这是我的代码:

$ch2=curl_init();
curl_setopt($ch2, CURLOPT_URL, $this->URL);
curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS,$this->XMLRequest);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch2, CURLOPT_SSLVERSION, 3); 
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);

$xml = curl_exec($ch2);

curl_close($ch2);
$file2 = fopen('upload/item.zip','w+');
fwrite($file2, $xml);
fclose($file2);

我也尝试过:

file_put_contents('upload/item.zip', $xml);

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:9)

尝试CURLOPT_FILEdownload large file

set_time_limit(0); //prevent timeout

$ch2=curl_init();
$file2 = fopen('upload/item.zip','w+');
curl_setopt($ch2, CURLOPT_URL, $this->URL);

curl_setopt($ch2, CURLOPT_FILE, $file2); //auto write to file

curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS,$this->XMLRequest);
# don't use this. please verify your host & peer properly :)
# curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
# curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch2, CURLOPT_SSLVERSION, 3); 
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);

curl_exec($ch2);

curl_close($ch2);
fclose($file2);

修改

注意:正如@bansi所指出的,您可能需要验证文件,文件大小,curl_error等。