我尝试了两种方法将文件添加到ZIP。但他们都没有工作。下载的ZIP大小为0字节。我有以下代码将文件从uploads / Applications文件夹添加到ZIP存档。
$zip = new ZipArchive();
$zip_name = "REU_Files.zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
die("Sorry ZIP creation failed at this time");
//首先尝试
$handle = opendir("uploads/Applications/");
while(false !== ($file = readdir($handle)))
if ($file != "." && $file != "..")
{
$zip->addFile("uploads/Applications/" . $file);
}
//第二次尝试
$zip->addEmptyDir("Applications/");
foreach (glob("uploads/Applications/*") as $file) {
$zip->addFile($file, "Applications/" . basename($file));
}
$zip->close();
//使zip可下载
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
请帮我查一下我的代码出错的地方。提前谢谢。