PHP 压缩文件夹

    /**
     * 循环压缩目录文件
     * @param $path 文件夹路径
     * @param $zip 压缩后zip名字及路径
     */
    function addFileToZip($path, $zip)
    {
        $handler = opendir($path); //打开当前文件夹由$path指定。
        while (($filename = readdir($handler)) !== false) {
            //文件夹文件名字为'.'和‘..’,不要对他们进行操作
            if ($filename != "." && $filename != "..") {
                // 如果读取的某个对象是文件夹,则递归
                if (is_dir($path . "/" . $filename)) {
                    addFileToZip($path . "/" . $filename, $zip);
                } else {
                    //将文件加入zip对象
                    $zip->addFile($path . "/" . $filename);
                    $zip->renameName($path . $filename, '11');
                }
            }
        }
        @closedir($path);
    }


    /**
     * 多目录压缩
     */
    function textDownload1()
    {
        $zip = new \ZipArchive();
        $username = $_SESSION['adminUser'];
        $zipname = $username . '.zip';
        $zipPath = './Public/doc/history/' . $zipname;
        if ($zip->open("$zipPath", \ZipArchive::CREATE) === TRUE) {
            $this->addFileToZip("./Public/doc/$username/", $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
            $zip->close(); //关闭处理的zip文件
            //设置打包完自动下载
            header('Content-Type: application/zip');
            header('Content-disposition: attachment; filename=' . $zipname);
            header('Content-Length: ' . filesize($zipPath));
            readfile($zipPath);
            unlink("./Public/doc/history/$username.zip");
        }
    }


    /**
     * 多文件压缩
     */
    function textDownload()
    {
        $username = $_SESSION['adminUser'];
        $zipname = $username . '.zip';
        $fileList = array(
            "./Public/doc/$username/answer.docx",
            "./Public/doc/$username/test.docx"
        );
        $filepath =  './Public/doc/history/' . $zipname;
        $zip = new \ZipArchive();
        $zip->open($filepath,\ZipArchive::CREATE);   //打开压缩包
        foreach($fileList as $file){
            $zip->addFile($file,basename($file));   //向压缩包中添加文件
        }
        $zip->close();  //关闭压缩包
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename=' . $zipname);
        header('Content-Length: ' . filesize($filepath));
        readfile($filepath);
    }


    /**
     * @param $path
     * 删除文件及目录
     */
    function deleteAll($path)
    {
        $op = dir($path);
        while (false != ($item = $op->read())) {
            if ($item == '.' || $item == '..') {
                continue;
            }
            if (is_dir($op->path . '/' . $item)) {
                deleteAll($op->path . '/' . $item);
                rmdir($op->path . '/' . $item);
            } else {
                unlink($op->path . '/' . $item);
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值