PHP/ThinkPHP实现批量打包下载文件的方法示例
作者:体验盒子 时间:2024-05-11 09:49:00
前言
本文主要给大家介绍的是关于PHP/ThinkPHP实现批量打包下载文件的相关内容,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍:
需求描述:
有数个文件,包含图片,文档。需要根据条件自动打包成压缩包,提供下载。
解决(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);
区别在引用的时候路径要对,结束。
相关参考:
http://www.php.net/manual/zh/class.ziparchive.php
http://dengrongguan12.github.io/blog/2016/php-ziparchive/
总结
好了,大概就这样,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持
来源:http://www.uedbox.com/php-ziparchive-download/
标签:php,批量打包,下载文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pandas 实现将NaN转换为None
2023-01-23 21:23:09
![](https://img.aspxhome.com/file/2023/8/131238_0s.png)
PyTorch深度学习模型的保存和加载流程详解
2023-07-10 04:58:33
![](https://img.aspxhome.com/file/2023/4/63194_0s.png)
vue3中路由传参query、params及动态路由传参详解
2023-07-02 16:58:29
Python中利用sqrt()方法进行平方根计算的教程
2022-09-17 13:11:25
Python类的动态修改的实例方法
2021-07-03 12:16:15
vue @click @tap重叠事件区分方式
2024-05-10 14:10:04
Django 限制用户访问频率的中间件的实现
2023-12-17 20:19:19
python 定义函数 返回值只取其中一个的实现
2022-07-29 21:28:21
![](https://img.aspxhome.com/file/2023/4/96914_0s.png)
安装PHP遇到“无法载入mysql扩展”解决方法
2007-06-15 15:04:00
Python基础之输入,输出与高阶赋值详解
2023-05-21 10:00:32
SQL Server 2008中的MERGE(不仅仅是合并)
2010-10-15 14:16:00
用python给csv里的数据排序的具体代码
2021-02-15 01:12:25
textarea 在IE和FF下换行无法正常显示的解决方法
2022-09-11 01:33:40
基于PHP的登录和注册的功能的实现
2024-04-30 08:48:04
![](https://img.aspxhome.com/file/2023/2/132792_0s.png)
正视WEB标准,一本全面的标准参考书
2009-05-30 16:36:00
![](https://img.aspxhome.com/file/UploadPic/20095/30/s3396721-91s.jpg)
详解Python下ftp上传文件linux服务器
2023-12-31 19:02:37
![](https://img.aspxhome.com/file/2023/0/90680_0s.png)
Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)
2021-09-14 00:22:11
![](https://img.aspxhome.com/file/2023/0/95540_0s.png)
一篇文章带你了解Python中的装饰器
2022-11-23 14:00:14
![](https://img.aspxhome.com/file/2023/6/112376_0s.jpg)
PyTorch模型转TensorRT是怎么实现的?
2021-09-01 08:14:02
![](https://img.aspxhome.com/file/2023/4/94704_0s.png)
Python获取一个用户名的组ID过程解析
2021-09-04 15:40:05