thinkphp 怎么将文件夹压缩成zip
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);
原文地址:https://www.cnblogs.com/zc290987034/p/12677695.html
时间: 2024-11-05 20:43:30