Table of Contents
这篇文章主要介绍了关于php打包zip图片下载的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下" >这篇文章主要介绍了关于php打包zip图片下载的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
1.引入类包 zipfile" >1.引入类包 zipfile
2.实例" >2.实例
Home Backend Development PHP Tutorial How to package zip image download in php

How to package zip image download in php

Jul 05, 2018 pm 03:34 PM
zip Image download

这篇文章主要介绍了关于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);
    }
}
Copy after login

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();
Copy after login

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

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

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

相关推荐:

php web请求安全处理

php cocket的使用方法

The above is the detailed content of How to package zip image download in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Best Guide to Compressing HTML Files to ZIP Best Guide to Compressing HTML Files to ZIP Apr 09, 2024 pm 04:09 PM

Compressing HTML files into ZIP can improve page loading speed. Methods include: using online tools (such as FileOptimizer, TinyPNG) using command line tools (such as gzip, 7-zip) using Node.js scripts (using the zlib module)

Detailed explanation of decompression file command (zip) under centos7 Detailed explanation of decompression file command (zip) under centos7 Jan 07, 2024 pm 06:30 PM

1. The compressed folder is a zip file [root@cgls]#zip-rmydata.zipmydata2. Unzip mydata.zip into the mydatabak directory [root@cgls]#unzipmydata.zip-dmydatabak3.mydata01 folder and mydata02.txt are compressed into mydata.zip[root@cgls]#zipmydata.zipmydata01mydata02.txt4. Decompress the mydata.zip file directly [root@cgls]#unzipmydata.zip5. View myd

How to use linux compression zip command How to use linux compression zip command Oct 08, 2023 pm 01:25 PM

The zip command is a very useful compression tool in Linux systems. By using the zip command, you can easily compress files and directories into a zip file and save storage space and facilitate transfer. The basic syntax of the zip command is "zip [options] [compressed file name] [file or directory to be compressed]".

How to use Zip function in Java for file compression How to use Zip function in Java for file compression Jun 26, 2023 pm 02:10 PM

Compressing files is a common operation that can save disk space and network transmission time, and Java provides the Zip function for file compression. This article will show how to use the Zip function in Java for file compression through a detailed introduction and example demonstration. 1. Introduction to the Zip function The Zip function is a compression and packaging tool library provided in Java. This function can be used to compress files or folders into a Zip format file. ZipOutputStr is mainly used in the Zip function

Easily master the secrets of ZIP compression of HTML files Easily master the secrets of ZIP compression of HTML files Apr 09, 2024 pm 05:36 PM

HTML file ZIP compression can be achieved through Python's zipfile module: Create a ZIP file object. Add HTML files to the ZIP file. Close the ZIP file object.

How to use Java to call Huawei Cloud OBS object storage service to download images How to use Java to call Huawei Cloud OBS object storage service to download images Jul 08, 2023 am 10:09 AM

How to use Java to call Huawei Cloud OBS Object Storage Service to download images Introduction: Huawei Cloud OBS (ObjectStorage Service) is a safe, reliable, highly scalable and low-cost cloud storage service. It provides flexible storage solutions that can store and retrieve large amounts of unstructured data, such as pictures, videos, documents, etc. in various scenarios. This article will introduce how to use the Java programming language to call Huawei Cloud OBS object storage service to implement the image download function. Step 1: Huawei Cloud

Which one is lossless, 7z or zip? Which one is lossless, 7z or zip? Jan 19, 2021 pm 06:37 PM

7z and zip are both lossless compressions. 7z is a mainstream and efficient compression format with a very high compression ratio; the ZIP file format is a file format for data compression and document storage. 7z has a higher compression ratio, followed by zip; the zip format is more common and has a wide range of technologies, and the windows operating system supports the zip format by default.

Crawl images from the website and automatically download them locally Crawl images from the website and automatically download them locally Jun 13, 2023 pm 01:28 PM

In the Internet age, people have become accustomed to downloading pictures from various websites such as galleries and social platforms. If you only need to download a small number of images, manual operation is not cumbersome. However, if a large number of pictures need to be downloaded, manual operation will become very time-consuming and laborious. At this time, automation technology needs to be used to automatically download the pictures. This article will introduce how to use Python crawler technology to automatically download images from the website to the local computer. This process is divided into two steps: the first step is to use Python's requests library or s

See all articles