[英]ZIP Archive sent by PHP is corrupted
我正在使用php ZipArchive動態創建一個zip文件並將其發送回用戶。 我暫時將壓縮文件存儲在文檔根目錄上的文件夾中,然后將其與代碼一起發回
header('Content-type:application/zip');
header('Content-Disposition: inline; filename="'.("file.zip").'"');
header("Content-Transfer-Encoding: binary");
header("Content-Length:".filesize($file));
$fh = fopen($file,'rb');
fpassthru($fh);
在第一次發布之后
$zip->close()
確保文件未打開。 我遇到的問題是 - 存儲的zip文件是一個有效的存檔,我可以在Windows 7,7Zip,WinZIP等中打開。但是,當我用上面的代碼向下發送文件時,它最終會有一個0xD 0xA對在文件的開頭,這足以使其損壞。 我無法弄清楚這些角色可能來自哪里。 這是fopen / fpassthru的已知錯誤嗎? 任何幫助將非常感激。
我在刪除header("Content-Length:".filesize($file));
時找到了header("Content-Length:".filesize($file));
它修復了我同樣的問題......
經過多次嘗試下載zip文件后,解決方案是:
$result = create_zip($files_to_zip,$fileZip,true,$path_parts['dirname']);
ob_clean();
ob_end_flush(); // more important function - (without - error corrupted
zip)
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header('Content-Type: application/zip;\n');
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"".basename($fileZip)."\"");
readfile($fileZip);
unlink($fileZip);
exit();
你的完整腳本是什么樣的?
一般來說,您應該刪除腳本文件末尾的任何結束PHP標記,因為它可能來自腳本末尾或包含的腳本。
謝謝@_on ! 你幫助我了解這些信息。 但不是刪除header ("Content-Length:". Filesize ($ file)) ;
,我插入了換行符“\\\\ n”,留下:
header ("Content-Length:". filesize ($ file). "\\n");
然后文件生成它的大小以幫助下載Okay。 TKS
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.