【发布时间】:2020-04-16 09:17:41
【问题描述】:
我正在尝试创建和下载 zip 文件,但我想将特定文件压缩到应从数据库中选择的目录中。
这个程序所做的只是从 school_homework 中选择这个主题的作业文件。
这实际上创建了一个 zip 文件,但是当我解压它时,它返回错误“此 zip 为空”!
while($row_file=mysqli_fetch_array($run_file)){
$school_homework_file_file=$row_file['school_homework_file_file'];
$zip = new ZipArchive();
$filename = "./homework.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$dir = "$school_homework_file_file";
// Create zip
createZip($zip,$dir);
$zip->close();
}
#Download Zip File
$filename = "homework.zip";
if (file_exists($filename)) {
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Length: ' . filesize($filename));
flush();
readfile($filename);
// delete file
unlink($filename);
}
}
// Create zip
function createZip($zip,$dir){
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
// If file
if (is_file($dir.$file)) {
if($file != '' && $file != '.' && $file != '..'){
$zip->addFile($dir.$file);
}
}else{
// If directory
if(is_dir($dir.$file) ){
if($file != '' && $file != '.' && $file != '..'){
// Add empty directory
$zip->addEmptyDir($dir.$file);
$folder = $dir.$file.'/';
// Read data of the folder
createZip($zip,$folder);
}
}
}
}
closedir($dh);
}
}
}
【问题讨论】:
-
嗨@daveek。您正在为每个目录创建相同的 zip 文件,并且每次都将其发送给用户。对吗?
-
实际上文件夹的名称是作业,当工作人员上传任何科目的作业时,它会存储在那里'../homework/80528242 - maths1.png'
-
我希望例如五年级的学生想下载数学科目的作业时,她/他可以下载该科目的所有作业!
-
可以,但您不能一次向用户发送多个 zip 文件。
-
我不想要那个,我想在一个导演(家庭作业/)中只压缩这些 .e.g. 5 个文件(从 db 查询的文件名)!
标签: php mysql zip ziparchive