PHP实现图片批量打包下载功能

上次遇到一个需要打包下载批量图片的问题,找了一下发现这个好方法,记录一下。

首先新建一个zipfile打包类:

  1. <?php
  2. classzipfile {
  3. var$datasec=array();
  4. var$ctrl_dir=array();
  5. var$eof_ctrl_dir="\x50\x4b\x05\x06\x00\x00\x00\x00";
  6. var$old_offset= 0;
  7. //phpfensi.com
  8. functionunix2_dostime($unixtime= 0){
  9. $timearray= ($unixtime== 0) ?getdate() :getdate($unixtime);
  10. if($timearray['year'] < 1980){
  11. $timearray['year'] = 1980;
  12. $timearray['mon'] = 1;
  13. $timearray['mday'] = 1;
  14. $timearray['hours'] = 0;
  15. $timearray['minutes'] = 0;
  16. $timearray['seconds'] = 0;
  17. }
  18. return(($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  19. }
  20. functionadd_file($data,$name,$time= 0){
  21. $name=str_replace('\\','/',$name);
  22. $dtime=dechex($this->unix2_dostime($time));
  23. $hexdtime='\x'.$dtime[6] .$dtime[7] .'\x'.$dtime[4] .$dtime[5] .'\x'.$dtime[2] .$dtime[3] .'\x'.$dtime[0] .$dtime[1];
  24. eval('$hexdtime = "'.$hexdtime.'";');
  25. $fr="\x50\x4b\x03\x04";
  26. $fr.="\x14\x00";
  27. $fr.="\x00\x00";
  28. $fr.="\x08\x00";
  29. $fr.=$hexdtime;
  30. $unc_len=strlen($data);
  31. $crc= crc32($data);
  32. $zdata= gzcompress($data);
  33. $zdata=<a href="/tags.php/substr/" target="_blank">substr</a>(substr($zdata, 0,strlen($zdata)- 4), 2);
  34. $c_len=strlen($zdata);
  35. $fr.= pack('V',$crc);
  36. $fr.= pack('V',$c_len);
  37. $fr.= pack('V',$unc_len);
  38. $fr.= pack('v',strlen($name));
  39. $fr.= pack('v', 0);
  40. $fr.=$name;
  41. $fr.=$zdata;
  42. $fr.= pack('V',$crc);
  43. $fr.= pack('V',$c_len);
  44. $fr.= pack('V',$unc_len);
  45. $this->datasec [] =$fr;
  46. $cdrec="\x50\x4b\x01\x02";
  47. $cdrec.="\x00\x00";
  48. $cdrec.="\x14\x00";
  49. $cdrec.="\x00\x00";
  50. $cdrec.="\x08\x00";
  51. $cdrec.=$hexdtime;
  52. $cdrec.= pack('V',$crc);
  53. $cdrec.= pack('V',$c_len);
  54. $cdrec.= pack('V',$unc_len);
  55. $cdrec.= pack('v',strlen($name));
  56. $cdrec.= pack('v', 0);
  57. $cdrec.= pack('v', 0);
  58. $cdrec.= pack('v', 0);
  59. $cdrec.= pack('v', 0);
  60. $cdrec.= pack('V', 32);
  61. $cdrec.= pack('V',$this->old_offset);
  62. $this->old_offset +=strlen($fr);
  63. $cdrec.=$name;
  64. $this->ctrl_dir[] =$cdrec;
  65. }
  66. functionadd_path($path,$l= 0){
  67. $d= @<a href="/tags.php/opendir/" target="_blank">opendir</a>($path);
  68. $l=$l> 0 ?$l:strlen($path) + 1;
  69. while($v= @readdir($d)){
  70. if($v=='.'||$v=='..'){
  71. continue;
  72. }
  73. $v=$path.'/'.$v;
  74. if(is_dir($v)){
  75. $this->add_path($v,$l);
  76. }else{
  77. $this->add_file(file_get_contents($v),substr($v,$l));
  78. }
  79. }
  80. }
  81. functionfile(){
  82. $data= implode('',$this->datasec);
  83. $ctrldir= implode('',$this->ctrl_dir);
  84. 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";
  85. }
  86. functionadd_files($files){
  87. <a href="/tags.php/foreach/" target="_blank">foreach</a>($filesas$file){
  88. if(is_file($file)){
  89. $data= implode("", file($file));
  90. $this->add_file($data,$file);
  91. }
  92. }
  93. }
  94. functionoutput($file){
  95. $fp=<a href="/tags.php/fopen/" target="_blank">fopen</a>($file,"w");
  96. fwrite($fp,$this->file ());
  97. fclose($fp);
  98. }
  99. }
  100. //下面是实例操作过程:
  101. $dfile= tempnam('/tmp','tmp');//产生一个临时文件,用于缓存下载文件
  102. $zip=newzipfile();
  103. //----------------------
  104. $filename='image.zip'//下载的默认文件名
  105. //以下是需要下载的图片数组信息,将需要下载的图片信息转化为类似即可
  106. $image=array(
  107. array('image_src'=>'pic1.jpg','image_name'=>'图片1.jpg'),
  108. array('image_src'=>'pic2.jpg','image_name'=>'pic/图片2.jpg'),
  109. );
  110. foreach($imageas$v){
  111. $zip->add_file(file_get_contents($v['image_src']),$v['image_name']);
  112. // 添加打包的图片,第一个参数是图片内容,第二个参数是压缩包里面的显示的名称, 可包含路径
  113. // 或是想打包整个目录 用 $zip->add_path($image_path);
  114. }
  115. //----------------------
  116. $zip->output($dfile);
  117. // 下载文件
  118. ob_clean();
  119. header('Pragma: public');
  120. header('Last-Modified:'.gmdate('D, d M Y H:i:s') .'GMT');
  121. header('Cache-Control:no-store, no-cache, must-revalidate');
  122. header('Cache-Control:pre-check=0, post-check=0, max-age=0');
  123. header('Content-Transfer-Encoding:binary');
  124. header('Content-Encoding:none');
  125. header('Content-type:multipart/form-data');
  126. header('Content-Disposition:attachment; filename="'.$filename.'"');//设置下载的默认文件名
  127. header('Content-length:'.filesize($dfile));
  128. $fp=fopen($dfile,'r');
  129. while(connection_status() == 0 &&$buf= @fread($fp, 8192)){
  130. echo$buf;
  131. }
  132. fclose($fp);
  133. @unlink($dfile);
  134. @flush();
  135. @ob_flush();
  136. exit();
  137. ?>