1 <?php 2 3 4 class Imagedown { 5 var $datasec = array (); 6 var $ctrl_dir = array (); 7 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; 8 var $old_offset = 0; 9 public function unix2_dostime($unixtime = 0){ 10 $timearray = ($unixtime == 0) ? getdate () : getdate($unixtime); 11 if ($timearray ['year'] < 1980){ 12 $timearray ['year'] = 1980; 13 $timearray ['mon'] = 1; 14 $timearray ['mday'] = 1; 15 $timearray ['hours'] = 0; 16 $timearray ['minutes'] = 0; 17 $timearray ['seconds'] = 0; 18 } 19 return (($timearray ['year'] - 1980) << 25) | ($timearray ['mon'] << 21) | ($timearray ['mday'] << 16) | ($timearray ['hours'] << 11) | ($timearray ['minutes'] << 5) | ($timearray ['seconds'] >> 1); 20 } 21 public function add_file($data, $name, $time = 0){ 22 $name = str_replace('\\', '/', $name); 23 $dtime = dechex($this->unix2_dostime($time)); 24 $hexdtime = '\x' . $dtime [6] . $dtime [7] . '\x' . $dtime [4] . $dtime [5] . '\x' . $dtime [2] . $dtime [3] . '\x' . $dtime [0] . $dtime [1]; 25 eval('$hexdtime = "' . $hexdtime . '";'); 26 $fr = "\x50\x4b\x03\x04"; 27 $fr .= "\x14\x00"; 28 $fr .= "\x00\x00"; 29 $fr .= "\x08\x00"; 30 $fr .= $hexdtime; 31 $unc_len = strlen($data); 32 $crc = crc32($data); 33 $zdata = gzcompress($data); 34 $zdata = substr(substr($zdata, 0, strlen($zdata)- 4), 2); 35 $c_len = strlen($zdata); 36 $fr .= pack('V', $crc); 37 $fr .= pack('V', $c_len); 38 $fr .= pack('V', $unc_len); 39 $fr .= pack('v', strlen($name)); 40 $fr .= pack('v', 0); 41 $fr .= $name; 42 $fr .= $zdata; 43 $fr .= pack('V', $crc); 44 $fr .= pack('V', $c_len); 45 $fr .= pack('V', $unc_len); 46 $this->datasec [] = $fr; 47 $cdrec = "\x50\x4b\x01\x02"; 48 $cdrec .= "\x00\x00"; 49 $cdrec .= "\x14\x00"; 50 $cdrec .= "\x00\x00"; 51 $cdrec .= "\x08\x00"; 52 $cdrec .= $hexdtime; 53 $cdrec .= pack('V', $crc); 54 $cdrec .= pack('V', $c_len); 55 $cdrec .= pack('V', $unc_len); 56 $cdrec .= pack('v', strlen($name)); 57 $cdrec .= pack('v', 0); 58 $cdrec .= pack('v', 0); 59 $cdrec .= pack('v', 0); 60 $cdrec .= pack('v', 0); 61 $cdrec .= pack('V', 32); 62 $cdrec .= pack('V', $this->old_offset); 63 $this->old_offset += strlen($fr); 64 $cdrec .= $name; 65 $this->ctrl_dir[] = $cdrec; 66 } 67 public function add_path($path, $l = 0){ 68 $d = @opendir($path); 69 $l = $l > 0 ? $l : strlen($path) + 1; 70 while($v = @readdir($d)){ 71 if($v == '.' || $v == '..'){ 72 continue; 73 } 74 $v = $path . '/' . $v; 75 if(is_dir($v)){ 76 $this->add_path($v, $l); 77 } else { 78 $this->add_file(file_get_contents($v), substr($v, $l)); 79 } 80 } 81 } 82 public function file(){ 83 $data = implode('', $this->datasec); 84 $ctrldir = implode('', $this->ctrl_dir); 85 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"; 86 } 87 public function add_files($files){ 88 foreach($files as $file){ 89 if (is_file($file)){ 90 $data = implode("", file($file)); 91 $this->add_file($data, $file); 92 } 93 } 94 } 95 public function output($file){ 96 $fp = fopen($file, "w"); 97 fwrite($fp, $this->file ()); 98 fclose($fp); 99 } 100 }View Code
以上是类文件 文件外进行调用
1 public function dowmload_img() 2 { 3 $img_id= $_GET["url"]; 4 print_r($img_id);exit(); 5 //打包下载实例操作过程 6 $dfile = tempnam('/tmp', 'tmp');//产生一个临时文件,用于缓存下载文件 7 require_once ROOT_PATH.'extend/PHPImagedown/Imagedown.php'; 8 $zip = new \Imagedown(); 9 //能访问到的路径 10 $image = array( 11 'http://a.cn/upimages/20191128/2019112815749059887920.jpg', 12 'http://a.cn/upimages/20191128/2019112815749059887920.jpg' 13 ); 14 15 $filename = 'image.zip'; //下载的默认文件名 16 foreach($image as $v){ 17 $zip->add_file(file_get_contents($v), basename($v)); 18 // 添加打包的图片,第一个参数是图片内容,第二个参数是压缩包里面的显示的名称, 可包含路径 19 // 或是想打包整个目录 用 $zip->add_path($image_path); 20 } 21 //---------------------- 22 $zip->output($dfile); 23 // 下载文件 24 ob_clean(); 25 header('Pragma: public'); 26 header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT'); 27 header('Cache-Control:no-store, no-cache, must-revalidate'); 28 header('Cache-Control:pre-check=0, post-check=0, max-age=0'); 29 header('Content-Transfer-Encoding:binary'); 30 header('Content-Encoding:none'); 31 header('Content-type:multipart/form-data'); 32 header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名 33 header('Content-length:'. filesize($dfile)); 34 $fp = fopen($dfile, 'r'); 35 while(connection_status() == 0 && $buf = @fread($fp, 8192)){ 36 echo $buf; 37 } 38 fclose($fp); 39 @unlink($dfile); 40 @flush(); 41 @ob_flush(); 42 exit(); 43 }图片打包下载