看起来像是在添加
解除链接($filename);
到底部似乎可以解决这个问题:
=====================================================================
所以我要做的就是用2个字符串创建一个zip文件,然后发送一些头文件,告诉浏览器打开一个“另存为”对话框,然后下载这个zip文件。
它工作得很好,但是当用户点击下载链接时,当弹出“另存为”对话框时,zip文件的副本将被复制到php文件夹的根目录中,其中就是php脚本所在的位置。有人知道为什么会这样吗?我认为问题可能出在标题上。我只想在用户指定的地方保存一份副本。
$string1 = "Some data Some data Some data Some data Some data Some data";
$string2 = "Some data Some data Some data Some data Some data Some data";
$filename = "test.zip";
$zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("string1.html", $string1);
$zip->addFromString("string2.html", $string2);
$zip->close();
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=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
clearstatcache();
header("Content-Length: ".filesize($filename));
readfile($filename);
?> 发布于 2013-03-08 08:03:26
这就是open($filename, ZIPARCHIVE::CREATE)要做的..。我会这样做:
->open($tempfile = tempnam(sys_get_temp_dir(),'yourzip_'), ZIPARCHIVE::CREATE)
....
header("Content-Disposition: attachment; filename=\"".$filename."\"");
...
header("Content-Length: ".filesize($tempfile));
readfile($tempfile);
unlink($tempfile);请注意,你的'test.zip‘文件名只需要在标题中,你可以给你的临时文件起任何你喜欢的名字。
发布于 2013-03-08 08:06:00
我认为只要你调用$zip->open(),文件就会被保存到你的服务器上。
我不太熟悉ZipArchive的工作方式,但如果它需要调用open()方法才能工作,您可以尝试使用php://memory协议,如下面的问题所述:
String to Zipped Stream in php
注意:以上链接仅与如何使用php://memory协议相关。
https://stackoverflow.com/questions/15284267
复制相似问题