Thinkphp6如何利用ZipArchive打包下载文件

 5275

下面给大家介绍Thinkphp6利用ZipArchive打包下载文件的方法,希望对需要的朋友有所帮助!

基础环境

系统环境:Windows10 x64

PHP集成环境:phpstudy

PHP依赖管理工具:Composer

手册:Thinkphp

下载tp6框架

  1. composer create-project topthink/think tp6

(二)打包下载类

  1. <?php
  2. namespace Jrk;
  3. class Zipdown
  4. {
  5.     /**
  6.      * 打包压缩文件及文件夹
  7.      *
  8.      * @Author Hhy <jackhhy520@qq.com>
  9.      * @DateTime 2020-07-10 13:20:06
  10.      * @param array $files
  11.      * @param string $zipName 压缩包名称
  12.      * @param boolean $wen 
  13.      * @param boolean $isDown
  14.      * @return void
  15.      **/
  16.     public function zip_file($files = [], $zipName = '', $wen = true,$isDown = true){
  17.         $zip_file_path='zip/';
  18.         // 文件名为空则生成文件名
  19.         if (empty($zipName)) {
  20.             $zipName = $zip_file_path.date('YmdHis') . '.zip';
  21.         }else{
  22.             $zipName=$zip_file_path.$zipName.'.zip';
  23.         }
  24.         // 实例化类,使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  25.         $zip = new \ZipArchive;
  26.         /*
  27.         * 通过ZipArchive的对象处理zip文件
  28.         * $zip->open这个方法如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE
  29.         * $zip->open这个方法第一个参数表示处理的zip文件名。
  30.         * 这里重点说下第二个参数,它表示处理模式
  31.         * ZipArchive::OVERWRITE 总是以一个新的压缩包开始,此模式下如果已经存在则会被覆盖。
  32.         * ZipArchive::OVERWRITE 不会新建,只有当前存在这个压缩包的时候,它才有效
  33.         * */
  34.         if ($zip->open($zipName, \ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE) !== true) {
  35.             exit('无法打开文件,或者文件创建失败');
  36.         }
  37.           // 文件夹打包处理
  38.         if (is_string($files)) {
  39.             // 文件夹整体打包
  40.             $this->addFileToZip($files, $zip);
  41.         } else {
  42.              // 文件打包
  43.             foreach ($files as $val) {
  44.                 if(file_exists(app()->getRootPath().'public'.$val['att_dir'])){
  45.                     if($wen){
  46.                         //根据存储的文件夹打包分层
  47.                         $zip->addFile(app()->getRootPath().'public'.$val['att_dir'], iconv('UTF-8','gbk',$val['img_dir'].'/'.$val['name']));
  48.                     }else{
  49.                         //不分层
  50.                         $zip->addFile(app()->getRootPath().'public'.$val['att_dir'], iconv('UTF-8','gbk',$val['name']));
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         // 关闭
  56.         $zip->close();
  57.         // 验证文件是否存在
  58.         if (!file_exists($zipName)) {
  59.             exit("文件不存在");
  60.         }
  61.         if ($isDown) {
  62.             // ob_clean();
  63.             // 下载压缩包
  64.             header("Cache-Control: public");
  65.             header("Content-Description: File Transfer");
  66.             header('Content-disposition: attachment; filename=' . basename($zipName)); //文件名
  67.             header("Content-Type: application/zip"); //zip格式的
  68.             header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  69.             header('Content-Length: ' . filesize($zipName)); //告诉浏览器,文件大小
  70.             @readfile($zipName);//ob_end_clean();
  71.             @unlink(app()->getRootPath().'public/'.$zipName);//删除压缩包
  72.         } else {
  73.             // 直接返回压缩包地址
  74.             return $zipName;
  75.         }
  76.     }
  77.     /**
  78.      * 添加文件至压缩包
  79.      * @Author Hhy <jackhhy520@qq.com>
  80.      * @DateTime 2020-07-10 13:20:26
  81.      * @param [type] $path
  82.      * @param [type] $zip
  83.      * @return void
  84.      */ 
  85.     public function addFileToZip($path, $zip)
  86.     {
  87.         // 打开文件夹
  88.         $handler = opendir($path);
  89.         while (($filename = readdir($handler)) !== false) {
  90.             if ($filename != "." && $filename != "..") {
  91.                 // 编码转换
  92.                 $filename = iconv('gb2312', 'utf-8', $filename);
  93.                 // 文件夹文件名字为'.'和‘..’,不要对他们进行操作
  94.                 if (is_dir($path . "/" . $filename)) {
  95.                     // 如果读取的某个对象是文件夹,则递归
  96.                     $this->addFileToZip($path . "/" . $filename, $zip);
  97.                 } else {
  98.                     // 将文件加入zip对象
  99.                     $file_path = $path . "/" . $filename;
  100.                     $zip->addFile($file_path, basename($file_path));
  101.                 }
  102.             }
  103.         }
  104.         // 关闭文件夹
  105.         @closedir($path);
  106.     }
  107.     /**
  108.      * 压缩文件解压
  109.      *
  110.      * @Author Hhy <jackhhy520@qq.com>
  111.      * @DateTime 2020-07-10 13:23:11
  112.      * @param [type] $file
  113.      * @param [type] $dirname
  114.      * @return void
  115.      */
  116.     public  function unzip_file($file, $dirname)
  117.     {
  118.         if (!file_exists($file)) {
  119.             return false;
  120.         }
  121.         // zip实例化对象
  122.         $zipArc = new \ZipArchive();
  123.         // 打开文件
  124.         if (!$zipArc->open($file)) {
  125.             return false;
  126.         }
  127.         // 解压文件
  128.         if (!$zipArc->extractTo($dirname)) {
  129.             // 关闭
  130.             $zipArc->close();
  131.             return false;
  132.         }
  133.         return $zipArc->close();
  134.     }
  135. }

(三)使用,亲测有效

  1. /**
  2.  * @author: Hhy <jackhhy520@qq.com>
  3.  * @date: 2020/7/2 0002
  4.  * @describe:打包下载
  5.  */
  6. public function download()
  7. {
  8.     $id= $this->request->param("id");
  9.     if (is_array($id)){
  10.         $ids=$id;
  11.     }else{
  12.         $ids=@explode(",",$id);
  13.     }
  14.     $data=$this->model->where('id', 'in',$ids)->select()->toArray();
  15.     //dd($data); 
  16.     if (empty($data)) {
  17.         $this->error("暂无数据");
  18.     }
  19.     $zip=new Jrk\Zipdown();
  20.     //打包下载
  21.     $zip->zip_file($data);
  22. }



本文网址:https://www.zztuku.com/detail-7918.html
站长图库 - Thinkphp6如何利用ZipArchive打包下载文件
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐