所以我有以下代码应该压缩文件夹并下载它,它确实有效,但当其中有另一个文件夹时,它会下载一个损坏的ZIP文件。我做错了什么?
此代码也在我的本地主机上创建zip文件,如何避免这种情况?
$getID = $_GET['id'];
$path = getFolderPath($getID);
$zip = new ZipArchive;
$zipName = time().".zip";
$zip->open($zipName, ZIPARCHIVE::CREATE);
if (false !== ($dir = opendir($path))){
while (false !== ($file = readdir($dir))){
if ($file != '.' && $file != '..'){
$zip->addFile($path.DIRECTORY_SEPARATOR.$file, $file);
}
}
}
$zip->close();
ob_get_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename='.$zipName);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zipName));
readfile($zipName);