下载zip PHP

Ale*_*eri 3 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);
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

file_put_contents('upload/item.zip', $xml);
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?

Lio*_*han 9

尝试CURLOPT_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);
Run Code Online (Sandbox Code Playgroud)

编辑:

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