我正在尝试将文件夹下载为 zip,但我总是得到一个 0 字节文件的 zip。我尝试了在互联网上找到的所有解决方案,但没有帮助。这是代码:
$files= scandir($dir);
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
if ( $zip->open($tmp_file, ZipArchive::CREATE)===TRUE) {
# loop through each file
foreach($files as $file){
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file),$download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header("Content-disposition: attachment; filename=$nome_dir.zip");
header("Content-length: " . filesize($tmp_file));
header('Content-type: application/zip');
readfile($tmp_file);
}
我不明白我错在哪里