PHP Package Zip image Download method

Source: Internet
Author: User

This article mainly introduces the PHP packaging zip image download method, has a certain reference value, now share to everyone, the need for friends can refer to

1. Introduction of Class Pack 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 [' Year '] < 1980} {$timearray [' year '] = 1980;            $timearray [' mon '] = 1;            $timearray [' mday '] = 1;            $timearray [' hours '] = 0;            $timearray [' minutes '] = 0;        $timearray [' seconds '] = 0; } return (($timearray [' Year ']-1980) << 25) | ($timearray [' mon '] << 21) | ($timearray [' Mday '] << 16) | ($timearray [' hours '] << 11) | ($timearray [' minutes '] << 5) |    ($timearray [' seconds '] >> 1);                } function Add_file ($data, $name, $time = 0) {$name = str_replace (' \ \ ', '/', $name);        $dtime = Dechex ($this->unix2_dostime ($time)); $hexdtime = ' \x '. $dtime [6]. $dtime [7]. ' \x '. $dtime [4]. $dtime [5]. ' \x '. $dtime [2]. $dtime [3]. ' \x '. $dtime [0].        $dtime [1]; Eval (' $hexdtime = '. $hexdtime.                '";');        $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 (' V ', $CRC);        $fr. = Pack (' V ', $c _len);        $fr. = Pack (' V ', $unc _len);        $fr. = Pack (' V ', strlen ($name));        $fr. = Pack (' V ', 0);                $fr. = $name;        $fr. = $zdata;        $fr. = Pack (' V ', $CRC);        $fr. = Pack (' V ', $c _len);                $fr. = Pack (' V ', $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 (' V ', $CRC);        $cdrec. = Pack (' V ', $c _len);        $cdrec. = Pack (' V ', $unc _len);        $cdrec. = Pack (' V ', strlen ($name));        $cdrec. = Pack (' V ', 0);        $cdrec. = Pack (' V ', 0);        $cdrec. = Pack (' V ', 0);        $cdrec. = Pack (' V ', 0);                $cdrec. = Pack (' V ', 32);        $cdrec. = Pack (' V ', $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 = = '. ' | | $v = = ' ... ')            {continue; } $v = $path. '/' .            $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 (' ', $this->datasec);        $ctrldir = Implode (' ', $this->ctrl_dir); Return $data. $ctrldir. $this->eof_ctrl_dir. Pack (' V ', sizeof ($this->ctrl_dir)). Pack (' V ', sizeof ($this->ctrl_dir)). Pack (' V ', strlen ($ctrldir)). Pack (' V ', 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. Instance

$dfile = Tempnam ('/tmp ', ' tmp ');//Generate a temporary file to cache the download file $zip = new \org\util\zipfile ();//----------------------$filename = ' QR code '. Date ("Y-m-d h:i", Time ()). Zip ';     Download the default file name//below is the picture array information that needs to be downloaded, convert the picture information that needs to be downloaded to similar can $params[' ids ' = Explode (', ', $params [' IDs ']);//Get Picture $image = Array ( Array (' image_src ' = ' pic1.jpg ', ' image_name ' + ' picture 1.jpg '), Array (' image_src ' = ' pic2.jpg ', ' image_name ' =& Gt    ' pic/image 2.jpg '), foreach ($image as $v) {$zip->add_file (file_get_contents ($v [' image_src ']), $v [' image_name ']); Add a packaged picture, the first parameter is the picture content, the second parameter is the name of the display in the compressed package, can contain the path//or want to package the entire directory with $zip->add_path ($image _path);} ----------------------$zip->output ($dfile);//download file Ob_clean (); header (' Pragma:public '); header (' Last-modified : '. Gmdate (' d, D M Y h:i:s '). ' GMT '); header (' Cache-control:no-store, No-cache, Must-revalidate '); header (' Cache-control:pre-check=0, post-check= 0, max-age=0 '); header (' content-transfer-encoding:binary '); header (' Content-encoding:none '); header (' Content-type: Multipart/form-datA '); header (' Content-disposition:attachment; Filename= '. $filename. '); Sets the default file name of the download header (' content-length: '. FileSize ($dfile)); $fp = fopen ($dfile, ' R '); while (connection_status () = = 0 & & $buf = @fread ($fp, 8192)) {echo $buf;} Fclose ($FP); @unlink ($dfile); @flush (); @ob_flush (); exit ();

It is worth mentioning that the package download when the Chinese folder or Chinese name garbled when the following methods can be resolved

$name = Iconv (' utf-8 ', ' gb2312 ', $name);

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.