2012-07-25 311 views
1

文件下載工作 - 除了大文件下載 - 大文件下載過快,打開時損壞 - 我得到錯誤信息(for壓縮文件):無法打開文件:它看起來不是有效的壓縮文件。該文件是高清上傳OK並且該文件夾php頭文件下載 - 無法打開文件:它似乎不是一個有效的檔案

在這是PHP代碼我使用強制頭下載

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 
exit; 
+0

您是否收到錯誤消息?例如。 abuot最大文件大小或時間? – PeeHaa 2012-07-25 09:30:34

+0

no - 不能看到任何錯誤 – Jeff 2012-07-25 09:33:39

+0

您是否啓用了錯誤報告? – PeeHaa 2012-07-25 09:34:14

回答

0

首先嚐試直接訪問文件(網址指向zip文件) 。

如果它也出現錯誤或損壞,請使用流式寫入器一次寫入大量數據。

(假設服務器端的文件沒有損壞)

或使用這樣的事

http://php.net/manual/en/httpresponse.send.php

+0

直接訪問文件時正確下載 – Jeff 2012-07-25 09:41:00

0

找到了解決辦法

這是一個最大內存分配的問題 - 它有一個PHP默認設置爲236M

我加了這個和它的工作

感謝所有幫助

問候

傑夫

+0

只提高內存分配限制是個壞主意。這意味着當有人試圖下載文件時,整個文件將被讀入內存,這可以在服務器上創建相當的性能。看看這個線程,你可以如何以塊的形式讀取文件,而不是隻將它的一小部分保存在內存中。 http://stackoverflow.com/questions/11647329/php-download-jpg-or-avi/11647520#11647520 – 2012-07-25 10:20:33

0

即使是加大了內存分配的下載量仍然相當不可靠的。我通過使用下面來管理它以保持穩定。欣賞每個人的意見。

header("Content-Disposition: attachment; filename=" . urlencode($file)); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Description: File Transfer");    
header("Content-Length: " . filesize($file)); 
flush(); // this doesn't really matter. 

$fp = fopen($file, "r"); 
while (!feof($fp)) 
{ 
echo fread($fp, 65536); 
flush(); // this is essential for large downloads 
} 
fclose($fp); 
相關問題