2013-09-24 516 views
3

我想要一個zip文件(我不知道大小和名稱之前)與請求xml,返回一個zip文件。我想下載它,但有時會下載它(16mb左右)有時不會(2mb或4mb或1mb)我不知道爲什麼。下載zip PHP

這是我的代碼:

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

有人能幫助我嗎?

+0

我沒有看到curl_exec在你的代碼?它是$ xml = curl_exec($ ch2); – Dexa

+0

編輯問題我錯過了一條線@Dexa –

回答

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

+1

我只是寫了同樣的答案,你打了我一秒。尼斯。 +1 – Dexa

+0

在確認下載的文件正確之前,請務必檢查是否有任何錯誤。 http://www.php.net/manual/en/function.curl-error.php – bansi

+0

不知道你是否需要設置'CURLOPT_TIMEOUT'嗯...是的需要驗證文件。 –