$filename= rand(1000, 9999).'.zip'; //最终生成的文件名
$zip=new ZipArchive();
$type = "images";
if($zip->open("./bookFile/{$type}/{$filename}",ZIPARCHIVE::CREATE)!==TRUE){
exit('无法打开文件,或者文件创建失败');
}
foreach ($bookIds as $bk => $bv) {
$id=$bv;
$file_n="book_".$id.".txt";
$fileContent = file_get_contents("./bookFile/{$type}/{$file_n}");//获取文件内容
if($fileContent){
$zip->addFromString(basename("./bookFile/{$type}/{$file_n}"), $fileContent);
}
}
$zip->close();//关闭
if(!file_exists("./bookFile/{$type}/{$filename}")){
exit('无法找到文件'); //即使创建,仍有可能失败
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=言蹊图书下载_'.$filename);
header('Content-Length: ' . filesize("./bookFile/{$type}/{$filename}"));
ob_end_clean();//清空(擦除)缓冲区并关闭输出缓冲
readfile("./bookFile/{$type}/{$filename}");
[
本帖最后由 xiexie 于 2019-10-17 09:11 编辑 ]