僅供參考,我已經閱讀並試圖在這些問題的答案,其他幾個線程:PHP下載腳本創建不可讀的ZIP文件在Mac
Creating and serving zipped files with php
Opening downloaded zip file creates cpgz file?
我有一個zip文件上我的服務器。
當我使用FileZilla中從我的服務器Zip文件移動到我的Mac,我可以正常打開。
當我使用此PHP代碼將Zip文件下載到我的Linux機器時,它正常打開。
當我使用此PHP代碼將Zip文件下載到我的Mac,使用Safari或Firefox時,出現「Decompression Failed」或「The archive of the archive is damaged」錯誤,或者我得到.cpgz文件 - 我相信這意味着計算機正在壓縮文件,而不是解壓縮文件。
這裏是我使用提供zip文件中的PHP代碼。
$zipname = "myfile.zip";
$zippath = "/path/to/" . $zipname;
if ($downloadzip = fopen ($zippath, "r")) {
$fsize = filesize($zippath);
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$zipname."\"");
header("Content-length: $fsize");
header('Content-Transfer-Encoding: binary');
#header("Cache-control: private"); //use this to open files directly
echo fpassthru($downloadzip); // deliver the zip file
}
fclose ($downloadzip);
我發現了一些工作的標題。我真的不知道或不在乎它爲什麼工作,我只是很高興它的工作原理...我嘗試了很多不同的東西,.htaccess文件,php.ini/zlib設置。
這裏的答案 http://perishablepress.com/http-headers-file-downloads/
$zipName = 'myfile.zip';
$zipPath = 'mydirectory/' . $zipName;
if (file_exists($zipPath)) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$zipName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zipPath));
ob_end_flush();
@readfile($zipPath);
}
當您更換'回聲fpassthru($ downloadzip)會發生什麼'?你試過刪除:'header('Content-Transfer-Encoding:binary');'? –
當我使用'readfile()'時,它以0.2 MB的方式進入,這是小的方式。我已經刪除了'header('Content-Transfer-Encoding:binary');'但沒有區別。 – Chris