【发布时间】:2012-09-06 20:22:41
【问题描述】:
作为参考,我已经阅读并尝试了这些和其他几个线程中的答案:
Creating and serving zipped files with php
Opening downloaded zip file creates cpgz file?
我的服务器上有一个 zip 文件。
当我使用 Filezilla 将该 Zip 文件从我的服务器移动到我的 Mac 时,我可以正常打开它。
当我使用此 PHP 代码将 Zip 文件下载到我的 Linux 机器时,它可以正常打开。
当我使用此 PHP 代码通过 Safari 或 Firefox 将 Zip 文件下载到我的 Mac 时,我收到一条错误消息,提示“解压缩失败”或“存档结构已损坏”,或者我得到一个 . 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);
}
【问题讨论】:
-
将
echo fpassthru($downloadzip);替换为readfile( $zippath );会发生什么?您是否尝试过删除:header('Content-Transfer-Encoding: binary');? -
当我使用
readfile()时,它的大小为 0.2 MB,这太小了。我已经删除了header('Content-Transfer-Encoding: binary');,但没有任何区别。