TP5.1批量下载无需外部插件进行批量下载zip
//获取所有下载文件ID
$idList = explode(",",$info['idList']);
//批量下载遍历文件地址
$files = [];
for($i = 0;$i<count($idList);$i++)
{
$contract = Contract::get($idList[$i]);
$downSrc = Env::get('root_path').'public'.$contract['contractfile'];
array_push($files,$downSrc);
}
//TP自带的类
$zip = new \ZipArchive;
//压缩文件名
$filename = Date('Y-m-d').'合同批量下载.zip';
//新建zip压缩包
$zip->open($filename,\ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE);
//循环压缩文件
foreach($files as $key => $value){
$fileStr = trim(strrchr($value,'/'),'/');//获取文件路径
$str = substr($fileStr,32);//获取后缀
//查询文件名称(若之前保存文件为源文件名保存可跳过该步骤)
$fileStr_b = Contract::where('contractfile','like','%'.$fileStr)->find()['contractname'];
$zip->addFile($value,basename($fileStr_b.$str));
}
//打包zip
$zip->close();
header("Cache-Control:public");
header("Content-Description: File Transfer");
header("Content-disposition: attachment; filename=".basename($filename));//文件名
header("Content-Type:application/zip"); //格式为zip
header("Content-Transfer-Encoding:binary"); //这是二进制文件
header("Content-Length:".filesize($filename)); //文件大小
@readfile($filename);