PHP/ThinkPHP实现批量打包下载文件的方法示例
实现批量打包下载文件的方法示例
前言前言
本文主要给大家介绍的是关于PHP/ThinkPHP实现批量打包下载文件的相关内容,分享出来供大家参考学习,话不多说了,来
一起看看详细的介绍:
需求描述:
需求描述:
有数个文件,包含图片,文档。需要根据条件自动打包成压缩包,提供下载。
解决(解决(ZipArchive 类):类):
PHP提供了ZipArchive 类可为我们实现这一功能,demo:
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/
总结总结
好了,大概就这样,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问
大家可以留言交流,谢谢大家对软件开发网的支持
您可能感兴趣的文章:php强制下载文件函数PHP下载文件的函数实例代码PHP实现远程下载文件到本地php实现当前页面点击
您可能感兴趣的文章
下载文件的实例代码php下载文件源代码(强制任意文件格式下载)php 文件下载 出现下载文件内容乱码损坏的解决方法(推
荐)php下载文件超时时间的设置方法PHP实现从远程下载文件的方法PHP下载文件函数与用法示例