【发布时间】:2010-11-29 00:07:07
【问题描述】:
我正在尝试使用以下代码从目录创建一个 zip 文件并通过 http 下载将其提供给用户:
// write the file
file_put_contents($path . "/index.html", $output);
// zip up the contents
chdir($path);
exec("zip -r {$course->name} ./");
$filename = "{$course->name}.zip";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' .urlencode($filename));
header('Content-Transfer-Encoding: binary');
readfile($filename);
我可以创建 zip 文件,但无法通过 http 下载。如果我下载使用 ftp 客户端创建的 zip 文件,那么 Mac 的 Stuffit Expander 会很好地解压缩文件,但如果我通过 http 下载它,mac 解压缩器会创建一个无限循环。我的意思是说我下载的文件名为 course.zip,然后解压缩该文件会得到 course.zip.cpgz,然后解压缩该文件会得到 course.zip 再次......等等。
有人有什么想法吗?
谢谢!
【问题讨论】:
-
您的 application/octet-stream 的 mime 类型没有错误,但使用 application/zip 可能更友好。
-
@Tony Miller - 为什么不把它作为答案呢?很可能是这里的问题。
-
如何从 php 进行“即时压缩和流式传输”:stackoverflow.com/a/4357904/416630