首页  >  文章  >  后端开发  >  php打包zip图片下载的方法

php打包zip图片下载的方法

不言
不言原创
2018-07-05 15:34:333786浏览

这篇文章主要介绍了关于php打包zip图片下载的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

1.引入类包 zipfile

class zipfile {
    var $datasec = array ();
    var $ctrl_dir = array ();
    var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
    var $old_offset = 0;
    
    function unix2_dostime($unixtime = 0){
        $timearray = ($unixtime == 0) ? getdate () : getdate($unixtime);        
        if ($timearray [&#39;year&#39;] < 1980){
            $timearray [&#39;year&#39;] = 1980;
            $timearray [&#39;mon&#39;] = 1;
            $timearray [&#39;mday&#39;] = 1;
            $timearray [&#39;hours&#39;] = 0;
            $timearray [&#39;minutes&#39;] = 0;
            $timearray [&#39;seconds&#39;] = 0;
        }
        return (($timearray [&#39;year&#39;] - 1980) << 25) | ($timearray [&#39;mon&#39;] << 21) | ($timearray [&#39;mday&#39;] << 16) | ($timearray [&#39;hours&#39;] << 11) | ($timearray [&#39;minutes&#39;] << 5) | ($timearray [&#39;seconds&#39;] >> 1);
    }
    function add_file($data, $name, $time = 0){
        $name = str_replace(&#39;\\&#39;, &#39;/&#39;, $name);
        
        $dtime = dechex($this->unix2_dostime($time));
        $hexdtime = &#39;\x&#39; . $dtime [6] . $dtime [7] . &#39;\x&#39; . $dtime [4] . $dtime [5] . &#39;\x&#39; . $dtime [2] . $dtime [3] . &#39;\x&#39; . $dtime [0] . $dtime [1];
        eval(&#39;$hexdtime = "&#39; . $hexdtime . &#39;";&#39;);
        
        $fr = "\x50\x4b\x03\x04";
        $fr .= "\x14\x00";
        $fr .= "\x00\x00";
        $fr .= "\x08\x00";
        $fr .= $hexdtime;
        
        $unc_len = strlen($data);
        $crc = crc32($data);
        $zdata = gzcompress($data);
        $zdata = substr(substr($zdata, 0, strlen($zdata)- 4), 2);
        $c_len = strlen($zdata);
        $fr .= pack(&#39;V&#39;, $crc);
        $fr .= pack(&#39;V&#39;, $c_len);
        $fr .= pack(&#39;V&#39;, $unc_len);
        $fr .= pack(&#39;v&#39;, strlen($name));
        $fr .= pack(&#39;v&#39;, 0);
        $fr .= $name;
        
        $fr .= $zdata;
        $fr .= pack(&#39;V&#39;, $crc);
        $fr .= pack(&#39;V&#39;, $c_len);
        $fr .= pack(&#39;V&#39;, $unc_len);
        
        $this->datasec [] = $fr;
        
        $cdrec = "\x50\x4b\x01\x02";
        $cdrec .= "\x00\x00";
        $cdrec .= "\x14\x00";
        $cdrec .= "\x00\x00";
        $cdrec .= "\x08\x00";
        $cdrec .= $hexdtime;
        $cdrec .= pack(&#39;V&#39;, $crc);
        $cdrec .= pack(&#39;V&#39;, $c_len);
        $cdrec .= pack(&#39;V&#39;, $unc_len);
        $cdrec .= pack(&#39;v&#39;, strlen($name));
        $cdrec .= pack(&#39;v&#39;, 0);
        $cdrec .= pack(&#39;v&#39;, 0);
        $cdrec .= pack(&#39;v&#39;, 0);
        $cdrec .= pack(&#39;v&#39;, 0);
        $cdrec .= pack(&#39;V&#39;, 32);
        
        $cdrec .= pack(&#39;V&#39;, $this->old_offset);
        $this->old_offset += strlen($fr);
        
        $cdrec .= $name;
        
        $this->ctrl_dir[] = $cdrec;
    }
    function add_path($path, $l = 0){
        $d = @opendir($path);
        $l = $l > 0 ? $l : strlen($path) + 1;
        while($v = @readdir($d)){
            if($v == &#39;.&#39; || $v == &#39;..&#39;){
                continue;
            }
            $v = $path . &#39;/&#39; . $v;
            if(is_dir($v)){
                $this->add_path($v, $l);
            } else {
                $this->add_file(file_get_contents($v), substr($v, $l));
            }
        }
    }
    function file(){
        $data = implode(&#39;&#39;, $this->datasec);
        $ctrldir = implode(&#39;&#39;, $this->ctrl_dir);
        return $data . $ctrldir . $this->eof_ctrl_dir . pack(&#39;v&#39;, sizeof($this->ctrl_dir)) . pack(&#39;v&#39;, sizeof($this->ctrl_dir)) . pack(&#39;V&#39;, strlen($ctrldir)) . pack(&#39;V&#39;, strlen($data)) . "\x00\x00";
    }
    
    function add_files($files){
        foreach($files as $file){
            if (is_file($file)){
                $data = implode("", file($file));
                $this->add_file($data, $file);
            }
        }
    }
    function output($file){
        $fp = fopen($file, "w");
        fwrite($fp, $this->file ());
        fclose($fp);
    }
}

2.实例

$dfile =  tempnam(&#39;/tmp&#39;, &#39;tmp&#39;);//产生一个临时文件,用于缓存下载文件
$zip = new \Org\Util\Zipfile();
//----------------------
$filename = &#39;二维码&#39;.date("Y-m-d H:i",time()).&#39;.zip&#39;; //下载的默认文件名
//以下是需要下载的图片数组信息,将需要下载的图片信息转化为类似即可
$params[&#39;ids&#39;] = explode(&#39;,&#39;,$params[&#39;ids&#39;]);

//获取图片
$image = array(
     array(&#39;image_src&#39; => &#39;pic1.jpg&#39;, &#39;image_name&#39; => &#39;图片1.jpg&#39;),
     array(&#39;image_src&#39; => &#39;pic2.jpg&#39;, &#39;image_name&#39; => &#39;pic/图片2.jpg&#39;),
);

foreach($image as $v){
    $zip->add_file(file_get_contents($v[&#39;image_src&#39;]),  $v[&#39;image_name&#39;]);
    // 添加打包的图片,第一个参数是图片内容,第二个参数是压缩包里面的显示的名称, 可包含路径
    // 或是想打包整个目录 用 $zip->add_path($image_path);
}
//----------------------
$zip->output($dfile);

// 下载文件
ob_clean();
header(&#39;Pragma: public&#39;);
header(&#39;Last-Modified:&#39;.gmdate(&#39;D, d M Y H:i:s&#39;) . &#39;GMT&#39;);
header(&#39;Cache-Control:no-store, no-cache, must-revalidate&#39;);
header(&#39;Cache-Control:pre-check=0, post-check=0, max-age=0&#39;);
header(&#39;Content-Transfer-Encoding:binary&#39;);
header(&#39;Content-Encoding:none&#39;);
header(&#39;Content-type:multipart/form-data&#39;);
header(&#39;Content-Disposition:attachment; filename="&#39;.$filename.&#39;"&#39;); //设置下载的默认文件名
header(&#39;Content-length:&#39;. filesize($dfile));
$fp = fopen($dfile, &#39;r&#39;);
while(connection_status() == 0 && $buf = @fread($fp, 8192)){
    echo $buf;
}
fclose($fp);
@unlink($dfile);
@flush();
@ob_flush();
exit();

值得一提的是 打包下载时中文文件夹或中文名出现乱码时可用下面方法解决

$name = iconv(&#39;utf-8&#39;,&#39;gb2312&#39;,$name);

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

php web请求安全处理

php cocket的使用方法

以上是php打包zip图片下载的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn