我有以下代码来推送 zip 文件以供下载。
$filename = "ResourcePack_".time().".zip";
$destination = $basepath."downloads/$filename";
if($this->createdownload($files,$destination,false)){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$destination").";");
header("Content-Disposition: attachment; filename='$filename'");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
ob_end_flush();
@readfile($destination);
if(file_exists($destination)){
unlink($destination);
}
}
我知道 createdownload 函数可以很好地生成 zip 文件,因为我看到该文件正在服务器上创建。问题是文件被作为一堆垃圾写入浏览器,而不是打开下载流。我的标题中是否缺少某些内容?
编辑
我是对的。我的问题不在于 php,而是通过 JQuery $.ajax 调用调用生成此代码的 php 文件是问题所在。使用 $.ajax 会自动将 Accept-Encoding 请求标头设置为与 zip 文件不兼容的值。因此,除了使用 $.ajax 之外,我只使用了一个简单的 window.open javascript 命令来调用相同的 php 页面,并且它与标题一起工作得很好。