我是PHP的新手,我正在尝试创建ZIP函数,该函数通过ajax调用并下载到用户。我得到了一个积极的回复zip但流媒体给我一个错误的文件大小():stat失败的../test.zip和读取文件无法打开流,没有这样的文件或目录在PHP页面上。这是我下面的代码 - 我很辛苦,似乎无法找到一个对我的情况有所了解的答案
<?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
发布于 2018-08-31 13:54:49
您无法使用addFile方法将文件夹的内容添加到zip文件中。您必须循环目录(http://php.net/dir)并使用addFile方法将每个文件添加到您的zip。
在将任何数据发送回用户之前,您可以检查zip文件是否存在。
$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文件的目录中具有写入权限。
https://stackoverflow.com/questions/-100008839
复制相似问题