[英]Zip file downloads, but is invalid?
我使用以下代碼使用戶能夠下載zip文件:
if(file_exists($filename)){
header("Content-Disposition: attachment; filename=".basename(str_replace(' ', '_', $filename)));
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($filename));
flush();
$fp = fopen($filename, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush();
}
fclose($fp);
exit;
}
下載文件時,它僅下載25,632千字節的數據。 但是,該zip文件的大小為26,252 KB。
為什么瀏覽器會占用全部25MB的內存,然后又停止?
我檢查了Content-Length
標頭,以確保它是正確的並且是...
在firefox中,當我下載文件時,它說“ 25mb”,因此瀏覽器認為25mb是“完整”量……但是,echo時的內容長度是26252904嗎?
在代碼之前添加
ob_clean();
ob_end_flush();
您的header('Content-Type ...)
調用無用,因為只有最后一個將被發送到瀏覽器。
下載由Content-Disposition: attachment
觸發。 如果要發送zip文件,則應發送實際的Content-Type: application/zip
。
最后,您的讀取循環是不必要的。
放在一起,您的代碼應如下所示:
if (file_exists($filename)) {
$quoted_filename = basename(addcslashes($filename, "\0..\37\"\177"));
header("Content-Disposition: attachment; filename=\"{$quoted_filename}\"");
header('Content-Type: application/zip');
header('Content-Length: '.filesize($filename));
readfile($filename);
}
使用單個MIME類型表示數據。
在這種情況下,使用application/octet-stream
就可以了。 這是您不事先知道MIME的時候。 當您知道它時,必須將其放置。 不要使用多個內容類型標題。
通常,當瀏覽器不知道如何處理特定的MIME時,它將觸發下載過程。 此外,使用Content-disposition: Attachment; ..
Content-disposition: Attachment; ..
確保它。
存在一個簡單的readfile($filename)
,它將把文件的字節發送到請求的進程,如下所示:
header("Content-disposition: attachment;filename=" . basename($filename);
readfile($filename);
我有類似的問題。 該文件在Firefox中可以很好地下載,但在IE中則不能。 看來Apache正在壓縮文件,而IE無法解壓縮,因此文件已損壞。 解決方案是在Apache中禁用gziping。 您還可以檢查PHP是否不是即時壓縮,也可以將其禁用。 對於Apache,您可以嘗試:
SetEnv no-gzip 1
對於PHP,在.htaccess中:
php_flag zlib.output_compression on
這個答案絕不是真正的答案。
但是我確實使它工作了……我只是將Content-Length設置為30000000。因此它認為文件大於實際大小,然后將其全部下載。
我知道丑陋的駭客,但我找不到其他方式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.