• 转:PHP 使用ZipArchive压缩文件并下载


    原文来自于:http://courages.us/archives/176

    网站上需要提供一些打印数据给用户下载,这些文件每次都需要重新生成,因为随时都会有新的数据产生。网络上关于PHP的压缩功能实现有多种方式,比如PclZip,ZipArchive等。PclZip是使用PHP写的一个压缩解压类,方便使用,不用安装扩展;而ZipArchive则在PHP 5.3之后随PHP携带发行,不需要再去开启扩展。本文则使用PHP自带的ZipArchive进行文档压缩。
    由于还要提供给用户下载,这里参考了网络上下载类,以下为downlaod.class.php代码

    <?php
     class download{
         protected $_filename;
         protected $_filepath;
         protected $_filesize;//文件大小
         public function __construct($filename){
             $this->_filename=$filename;
             $this->_filepath=dirname(__FILE__).'/'.$filename;
        }
        public function __destruct() {
            if (file_exists($this->_filepath)){
                unlink($this->_filepath);
            }
        }
        //获取文件名
        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("Accept-Ranges: bytes");
                //返回文件的大小
                Header("Accept-Length: ".filesize($this->_filepath));
                //这里对客户端的弹出对话框,对应的文件名
                Header("Content-Disposition: attachment; filename=".$this->_filename);
                //修改之前,一次性将数据传输给客户端
                echo fread($filefilesize($this->_filepath));
                //修改之后,一次只传输1024个字节的数据给客户端
                //向客户端回送数据
                $buffer=1024;//
                //判断文件是否读完
                while (!feof($file)) {
                    //将文件读入内存
                    $file_data=fread($file,$buffer);
                    //每次向客户端回送1024个字节的数据
                    echo $file_data;
                }
                fclose($file);
            }else {
                echo "<script>alert('对不起,您要下载的文件不存在');</script>";
            }
        }
    }

    ZipArchive有两个添加文件的方法addFile 和addFromString,addFile可将现有文件添加进去,而addFromStrin则可以添加一些动态的文本 。在要下载文件中则简单调用,代码如下

    <?php
    include 'download.class.php';
    $res='';//动态结果
     
    $zip=new ZipArchive();
    $zipname=date('YmdHis',time()).'.zip';//随机文件名
    if (!file_exists($zipname)){
        $zip->open($zipname,ZipArchive::OVERWRITE);//创建一个空的zip文件
        //$zip->addFile('/images/show.pic','show.pic');//将/images/show.pic文件添加进去
        //$content=file_get_contents('http://127.0.0.1/data.php?r='$res);
        //if ( $content !== false ) {
        //$zip->addFromString('data.xml',$content);//使用字符串的方式动态添加
        //}
        $zip->addFromString('test.txt''some text ...');
        $zip->close();
        $dw=new download($zipname); //下载文件
        $dw->getfiles();
        unlink($zipname); //下载完成后要主动删除   
    }

  • 相关阅读:
    flask总结02
    flask总结01
    恩智浦Freescale Cortex-A9 迅为IMX6开发板平台初体验
    [分享] IMX6嵌入式开发板linux QT挂载U盘及TF卡
    迅为4412嵌入式安卓开发板兼容3G网络|4G网络
    迅为嵌入式4412平台兼容3G/4G模块的安卓开发板
    飞思卡尔开发板-迅为IMX6开兼容单核 双核 四核Plus开发板
    物联网初学者智能家居必备迅为iTOP-4412开发板
    【分享】4412开发板POP烧写ubuntu出错,如何挂载emmc分区解决方法
    [安卓开发板]迅为IMX6 四核Android开发板
  • 原文地址:https://www.cnblogs.com/guoyongrong/p/3877733.html
Copyright © 2020-2023  润新知