首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP下载使用ziparchive创建的zip

PHP下载使用ziparchive创建的zip
EN

Stack Overflow用户
提问于 2018-08-31 04:03:41
回答 1查看 0关注 0票数 0

我是PHP的新手,我正在尝试创建ZIP函数,该函数通过ajax调用并下载到用户。我得到了一个积极的回复zip但流媒体给我一个错误的文件大小():stat失败的../test.zip和读取文件无法打开流,没有这样的文件或目录在PHP页面上。这是我下面的代码 - 我很辛苦,似乎无法找到一个对我的情况有所了解的答案

代码语言:javascript
复制
    <?php
    require("../../common.php");
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);
$thisdir = " ";
$zip = new ZipArchive();
$filename = "../test.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}
$zip->addFromString('insert.pdf' . time(), "#1 This is a test string added as testfilephp.txt.\n");;
$zip->addFile($filename . "../../../folder/pdf_folder/");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
echo $filename;
//$size = filesize($zip->filename);
$zip->close();
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$filename");
header("Content-length: " . filesize($filename));
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile("$filename");
?>

感谢您的帮助或指导

******编辑这里是代码的错误

警告:在第21行的/var/---/----/---//in/control/ajax/zipAJAX.php中的../test.zip中的 filesize():stat失败警告:readfile (../test.zip):无法打开流:/ var/---/----/---/----/control/ajax/zipAJAX.php上没有这样的文件或目录24

EN

回答 1

Stack Overflow用户

发布于 2018-08-31 13:54:49

您无法使用addFile方法将文件夹的内容添加到zip文件中。您必须循环目录(http://php.net/dir)并使用addFile方法将每个文件添加到您的zip。

在将任何数据发送回用户之前,您可以检查zip文件是否存在。

代码语言:javascript
复制
$zip->close();
if(file_exists($file)) {
  header("Content-type: application/zip"); 
  header("Content-Disposition: attachment; filename=$filename");
  header("Content-length: " . filesize($filename));
  header("Pragma: no-cache"); 
  header("Expires: 0"); 
  readfile("$filename");
}

还要检查用户是否在要放置zip文件的目录中具有写入权限。

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

https://stackoverflow.com/questions/-100008839

复制
相关文章

相似问题

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