时间:2021-07-01 10:21:17 帮助过:24人阅读
解决(ZipArchive 类):
PHP提供了ZipArchive 类可为我们实现这一功能,demo:
<?php $files = array('image.jpeg','text.txt','music.wav'); $zipname = 'enter_any_name_for_the_zipped_file.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); ///Then download the zipped file. header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: ' . filesize($zipname)); readfile($zipname); ?>
ThinkPHP版
$zip = new \ZipArchive; //压缩文件名 $filename = 'download.zip'; //新建zip压缩包 $zip->open($filename,\ZipArchive::OVERWRITE); //把图片一张一张加进去压缩 foreach ($images as $key => $value) { $zip->addFile($value); } //打包zip $zip->close(); //可以直接重定向下载 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);
区别在引用的时候路径要对,结束。
相关推荐:
React 和Webpack构建打包优化实例详解
parcel.js打包出错到选择nvm的全部过程解析
vue打包后显示空白如何处理
以上就是ThinkPHP实现批量打包下载文件的详细内容,更多请关注Gxl网其它相关文章!