thinkPHP 将多个文件合并成压缩包、此功能是依赖于thinkPHP
//要合并压缩的文件 $files[0] = 'Uploads/baojia_excel/nihao【CG20200402001】采购单(1).xls'; $files[1] = 'Uploads/baojia_excel/分类【CG20200402001】采购单(2).xls'; //这里需要注意该目录是否存在,并且有创建的权限 创建test.zip压缩包 $filename = 'Uploads/baojia_excel/test.zip'; $zip = new \ZipArchive; $res = $zip->open($filename, \ZipArchive::CREATE); if ($res === TRUE) { foreach ($files as $file) { //这里直接用原文件的名字进行打包,也可以直接命名,需要注意如果文件名字一样会导致后面文件覆盖前面的文件,所以建议重新命名 $new_filename = substr($file, strrpos($file, '/') + 1); $zip->addFile($file, $new_filename); } } //打包zip $aa = $zip->close(); //dump($aa);die(); //可以直接重定向下载 header('Location:'.$filename); //或者输出下载 header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename='.basename($filename)); //文件名 header("Content-Type: application/force-download"); header("Content-transfer-encoding: binary"); header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小 readfile($filename);