ThinkPHP实现将本地文件打包成zip下载
网络编程 2021-07-05 09:50www.168986.cn编程入门
这篇文章主要介绍了ThinkPHP实现将本地文件打包成zip下载的方法,需要的朋友可以参考下
首先,将FileToZip.class文件放到ThinkPHP/Extend/Library/ORG/Util/文件夹中,FileToZip.class.php为zip下载类,其详细代码如下:
<?php /** * zip下载类文件 * 遍历目录,打包成zip格式 */ class traverseDir{ public $currentdir;//当前目录 public $filename;//文件名 public $fileinfo;//用于保存当前目录下的所有文件名和目录名以及文件大小 public $savepath; public function __construct($curpath,$savepath){ $this->currentdir=$curpath;//返回当前目录 $this->savepath=$savepath;//返回当前目录 } //遍历目录 public function scandir($filepath){ if (is_dir($filepath)){ $arr=scandir($filepath); foreach ($arr as $k=>$v){ $this->fileinfo[$v][]=$this->getfilesize($v); } }else { echo "<script>alert('当前目录不是有效目录');</script>"; } } /** * 返回文件的大小 * * @param string $filename 文件名 * @return 文件大小(KB) */ public function getfilesize($fname){ return filesize($fname)/1024; } /** * 压缩文件(zip格式) */ public function tozip($items){ $zip=new ZipArchive(); $zipname=date('YmdHis',time()); if (!file_exists($zipname)){ $zip->open($savepath.$zipname.'.zip',ZipArchive::OVERWRITE);//创建一个空的zip文件 for ($i=0;$i<count($items);$i++){ $zip->addFile($this->currentdir.'/'.$items[$i],$items[$i]); } $zip->close(); $dw=new download($zipname.'.zip',$savepath); //下载文件 $dw->getfiles(); unlink($savepath.$zipname.'.zip'); //下载完成后要进行删除 } } } /** * 下载文件 * */ class download{ protected $_filename; protected $_filepath; protected $_filesize;//文件大小 protected $savepath;//文件大小 public function __construct($filename,$savepath){ $this->_filename=$filename; $this->_filepath=$savepath.$filename; } //获取文件名 public function getfilename(){ return $this->_filename; } //获取文件路径(包含文件名) public function getfilepath(){ return $this->_filepath; } //获取文件大小 public function getfilesize(){ return $this->_filesize=number_format(filesize($this->_filepath)/(1024*1024),2);//去小数点后两位 } //下载文件的功能 public function getfiles(){ //检查文件是否存在 if (file_exists($this->_filepath)){ //打开文件 $file = fopen($this->_filepath,"r"); //返回的文件类型 Header("Content-type: application/octet-stream"); //按照字节大小返回 Header("Aept-Ranges: bytes"); //返回文件的大小 Header("Aept-Length: ".filesize($this->_filepath)); //这里对客户端的弹出对话框,对应的文件名 Header("Content-Disposition: attachment; filename=".$this->_filename); //修改之前,一次性将数据传输给客户端 echo fread($file, filesize($this->_filepath)); //修改之后,一次只传输1024个字节的数据给客户端 //向客户端回送数据 $buffer=1024;// //判断文件是否读完 while (!feof($file)) { //将文件读入内存 $file_data=fread($file,$buffer); //每次向客户端回送1024个字节的数据 echo $file_data; } fclose($file); }else { echo "<script>alert('对不起,您要下载的文件不存在');</script>"; } } }
ThinkPHP中加载zip下载类FileToZip.class.php并实现本地文件打包下载的功能代码如下所示:
import('ORG.Util.FileToZip');//引入zip下载类文件FileToZip // 打包下载 $handler = opendir($cur_file); //$cur_file 文件所在目录 $download_file = array(); $i = 0; while( ($filename = readdir($handler)) !== false ) { if($filename != '.' && $filename != '..') { $download_file[$i++] = $filename; } } closedir($handler); $scandir=new traverseDir($cur_file,$save_path); //$save_path zip包文件目录 $scandir->tozip($download_file);
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程