问题描述
为了参考,我已经阅读并尝试了这些和其他几个线程中的答案:
我的服务器上有一个zip文件。
-
当我使用Filezilla将Zip文件从我的服务器移动到我的Mac时,我可以正常打开它。
-
当我使用这个PHP代码将Zip文件下载到我的Linux机器时,它会正常打开。
-
当我使用这个PHP代码将Zip文件下载到我的Mac时,使用Safari或Firefox,我收到一个错误,说解压失败或存档的结构损坏或者我得到一个.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); //使用这个直接打开文件
echo fpassthru($ downloadzip); //提供zip文件
}
fclose($ downloadzip);
我发现有些标题有效。我真的不知道或关心它为什么工作,我只是很高兴它的工作...我尝试了大量不同的东西.htaccess文件,php.ini / zlib设置。
这是答案
$ 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);
}
p>
$ 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);
}
For reference, I have already read and tried the answers in these and several other threads:
Creating and serving zipped files with php
Opening downloaded zip file creates cpgz file?
I have a zip file on my server.
When I use Filezilla to move that Zip file from my server to my Mac, I can open it normally.
When I use this PHP code to download the Zip file to my Linux machine, it opens normally.
When I use this PHP code to download the Zip file to my Mac, using Safari or Firefox, I get an error saying "Decompression Failed" or "The structure of the archive is damaged" or I get a .cpgz file - which I believe means that the computer is zipping the file, not unzipping it.
Here is the PHP code I am using to deliver the zip file.
$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);
I found some headers that work. I don't really know or care why it work, I am just happy it works... I tried a ton of different things, .htaccess files, php.ini / zlib settings.
Here's the answerhttp://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);
}
Here is what works
$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);
}
这篇关于PHP下载脚本在Mac上创建无法读取的ZIP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!