thinkphp 打包文件,并下载
/**
* 打包下载
* 注意文件路径都是相对路径
* 不能使用ajax的方式
* @return [type] [description]
*/
public function allzip(){
$param = $this->request->param();
$ids = explode(",",$param['id']);
//要打包的文件
foreach ($ids as $k => $v){
$files[] =substr(db('seofile')->where('id',$v)->value('fileurl'),1);
}
$zip = new \ZipArchive;
//压缩文件名
$filename = 'upload/file/download.zip';
//新建zip压缩包
$zip->open($filename,\ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE);
//循环压缩文件
foreach($files as $key => $value){
$zip->addFile($value,basename($value));
}
//打包zip
$zip->close();
header("Cache-Control:public");
header("Content-Description: File Transfer");
header("Content-disposition: attachment; filename=".basename($filename));//文件名
//header("Content-Type: application/force-download");
header("Content-Type:application/zip"); //格式为zip
header("Content-Transfer-Encoding:binary"); //这是二进制文件
header("Content-Length:".filesize($filename)); //文件大小
@readfile($filename);
// @unlink($filename);
}