首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP ZIP存档在用户选择保存位置之前被复制到根目录

问PHP ZIP存档在用户选择保存位置之前被复制到根目录
EN

Stack Overflow用户
提问于 2013-03-08 07:57:23
回答 2查看 654关注 0票数 1

看起来像是在添加

解除链接($filename);

到底部似乎可以解决这个问题:

=====================================================================

所以我要做的就是用2个字符串创建一个zip文件,然后发送一些头文件,告诉浏览器打开一个“另存为”对话框,然后下载这个zip文件。

它工作得很好,但是当用户点击下载链接时,当弹出“另存为”对话框时,zip文件的副本将被复制到php文件夹的根目录中,其中就是php脚本所在的位置。有人知道为什么会这样吗?我认为问题可能出在标题上。我只想在用户指定的地方保存一份副本。

代码语言:javascript
复制
$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);


?> 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-08 08:03:26

这就是open($filename, ZIPARCHIVE::CREATE)要做的..。我会这样做:

代码语言:javascript
复制
->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‘文件名只需要在标题中,你可以给你的临时文件起任何你喜欢的名字。

票数 1
EN

Stack Overflow用户

发布于 2013-03-08 08:06:00

我认为只要你调用$zip->open(),文件就会被保存到你的服务器上。

我不太熟悉ZipArchive的工作方式,但如果它需要调用open()方法才能工作,您可以尝试使用php://memory协议,如下面的问题所述:

String to Zipped Stream in php

注意:以上链接仅与如何使用php://memory协议相关。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15284267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档