php 文件压缩和下载类封装

<?php
/**
 * 关于文件压缩和下载的类
 * @author  qq274805539
 * @version 1.0
 */
class zip_down{

    protected $file_path;
    /**
     * 构造函数
     * @param [string] $path [传入文件目录]
     */
    public function __construct($path){
        $this->file_path=$path; //要打包的根目录
    }
    /**
     * 入口调用函数
     * @return [type] [以二进制流的形式返回给浏览器下载到本地]
     */
    public function index($wid){
        $zip=new ZipArchive();
        $end_dir=$this->file_path.date('Ymd').'_'.$wid.'_template.zip';//定义打包后的包名
        $dir=$this->file_path;
        if(!is_dir($dir)){
            mkdir($dir);
        }
        if($zip->open($end_dir, ZipArchive::OVERWRITE) === TRUE){ ///ZipArchive::OVERWRITE 如果文件存在则覆盖
            $this->addFileToZip($dir, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
            $zip->close();
        }
        if(!file_exists($end_dir)){
            exit("无法找到文件");
        }
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Type: application/zip"); //zip格式的
        header('Content-disposition: attachment; filename='.basename($end_dir)); //文件名
        header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
        header('Content-Length:'.filesize($end_dir)); //告诉浏览器,文件大小
        @readfile($end_dir);
        $this->delDirAndFile($dir,true);//删除目录和文件
        unlink($end_dir);删除压缩包
    }
    /**
     * 文件压缩函数 需要开启php zip扩展
     * @param [string] $path [路径]
     * @param [object] $zip  [扩展ZipArchive类对象]
     */
    protected function addFileToZip($path, $zip){
        $handler = opendir($path);
        while (($filename=readdir($handler)) !== false) {
            if ($filename!= "." && $filename!=".."){
                if(is_dir($path."/".$filename)){
                    $this->addFileToZip($path."/".$filename,$zip);
                } else {
                    $zip->addFile($path."/".$filename);
                }
            }
        }
        @closedir($path);
    }
    /**
     * 删除文件函数
     * @param  [string]  $dir    [文件目录]
     * @param  boolean $delDir [是否删除目录]
     * @return [type]          [description]
     */
    protected function delDirAndFile($path,$delDir=true){
        $handle=opendir($path);
        if($handle){
            while(false!==($item = readdir($handle))){
                if($item!="."&&$item!=".."){
                    if(is_dir($path.'/'.$item)){
                        $this->delDirAndFile($path.'/'.$item, $delDir);
                    }else{
                        unlink($path.'/'.$item);
                    }
                }
            }
            @closedir($handle);
            if($delDir){return rmdir($path);}
        }else{
            if(file_exists($path)){
                return unlink($path);
            }else{
                return FALSE;
            }
        }
    }

}

//使用方法:
//打包文件夹里面文件
$zip = new zip_down($path_dir); //传递需要打包的文件夹目录
$zip->index($wid);//打包文件  


我安装laravel的一个扩展时,提示没有ZipArchive 我根据国外的一个网站的提示进行操作,任然不成功,后重新修改了这个包才成功 首先要执行下面操作 brew update brew install php@7.3 brew link php@7.3 然后执行 Step 1: Install PEAR/PECL support cd /tmp curl -s -O https://pear.php.net/install-pear-nozlib.phar sudo php install-pear-nozlib.phar -d /usr/local/lib/php -b /usr/local/bin (credit) Step 2: Download zip extension source I tried installing zip with pecl, but that failed because the PHP header files were missing. So I downloaded the source for the zip extension from pecl. Step 3:下载我提供的包文件 然后解压 Step 4: Copy/modify phpsize and php-config In order to make phpize and php-config find the PHP header files in the location that XCode places them rather than in /usr/include/php -- a read-only location now -- I followed some tips I found elsewhere and copied and modified phpize and php-config to change the include directory in each of them. cp /usr/bin/phpize /usr/local/bin/phpize cp /usr/bin/php-config /usr/local/bin/php-config Step 5: Build the zip PHP extension With all of those changes in place, I could now build the zip extension. cd zip-1.15.5 phpize ./configure -with-php-config=/usr/local/bin/php-config make Step 6: Install zip PHP extension make install fails to install the extension, again because of the read-only file system. So instead I created an extension directory under /usr/local/php. mkdir -p /usr/local/php/extensions cp modules/zip.so /usr/local/php/extensions/zip.so Step 7: Update your PHP.ini Finally we need to tell PHP to load this extension. sudo vim /etc/php.ini Add the following line: extension=/usr/local/php/extensions/zip.so Restart apache with sudo apachectl restart and you'll see the zip extension being loaded now.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值