• thinkphp文件夹生成zip压缩包


    一、准备工作,使用phpinfo()查看有没有zip扩展

    
    echo phpinfo();
    ?>
    
    • 1
    • 2
    • 3

    Thinkphp使用PHP自带的ZipArchive压缩文件或文件夹
    在这里插入图片描述显示enabled 说明已经配置好

    如果没有安装扩展的,请参照以下方法:

    1、下载对应版本的扩展包:https://windows.php.net/downloads/pecl/releases/zip/1.20.0/,比如我是php7.4(window7_64位系统),则下载如下:
    在这里插入图片描述Thinkphp使用PHP自带的ZipArchive压缩文件或文件夹

    2、把解压的php_zip.dll文件放到php-5.6.27-nts/ext目录

    3、在php.ini添加以下配置:

    extension=php_zip.dll

    4、重启Apache

    二、在控制器中使用

    //文件夹打包
    public function zip()
    {
        $local = app()->getRootPath().'\public';
        try {
            //文件夹目录
            $dirPath = $local."\demo";
            //zip压缩包保存路径
            $zipPath = $local."\demo.zip";
            //创建zip实例
            $zip=new \ZipArchive();
            if($zip->open($zipPath, \ZipArchive::CREATE|\ZipArchive::OVERWRITE)=== TRUE) {
                //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
                $this->addFileToZip($zip,$dirPath,$dirPath);
                //关闭处理的zip文件
                $zip->close();
            }
        }
        catch (\Exception $e) {
        	//echo '压缩失败';
            halt($e);
        }
        //echo '压缩成功';
        
        $this->downloadZip($zipPath);
    }
    
    
    //下载function
    public function downloadZip($zipPath)
    {
        $zipPath = iconv("UTF-8", "GBK", $zipPath);//加这行中文文件夹也ok了
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename=' . basename($zipPath)); //文件名
        header("Content-Type: application/zip"); //zip格式的
        header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
        header('Content-Length: ' . filesize($zipPath)); //告诉浏览器,文件大小
        @readfile($zipPath);//ob_end_clean();
        @unlink(app()->getRootPath().'public/'.$zipPath);//删除压缩包
    }
    
    //压缩包追加文件
    public function addFileToZip($zip,$path,$root){
       $handler=opendir($path); //打开当前文件夹
       while(($filename=readdir($handler))!==false){
           if($filename != "." && $filename != ".."){//不操作名字为'.'和'..'的文件夹或文件
               if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
                   $this->addFileToZip($zip,$path."/".$filename,$root);
               }else{
                   //将文件加入zip对象,第二个参数是zip里文件的路径
                   $pathFilename=$path . "/" . $filename;
                   $zip->addFile($pathFilename, str_replace($root.'/','',$pathFilename));
               }
           }
       }
       //@closedir($path);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    小伙伴们,以上亲测有效,记得关注

  • 相关阅读:
    cartographer中创建轨迹
    国家开放大学 模拟试题 训练
    构建卓越语言模型应用的利器:LangChain | 开源日报 No.39
    05-Redis高可用集群之水平扩展
    Java调用Web Service接口
    大数据之Spark(一)
    软考高项-十大知识领域五大过程组图
    代码随想录刷题记录 4 - 字符串
    Cholesterol-PEG-Acid,Cholesterol-PEG-COOH,疏水性分子胆固醇-聚乙二醇-羧基
    【力扣-数据结构和算法-头哨兵】移除链表元素
  • 原文地址:https://blog.csdn.net/weixin_47736740/article/details/134562692