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

php多张图片打包下载的更多相关文章

  1. 2014年最新720多套Android源码2.0GB免费一次性打包下载

    之前发过一个帖子,但是那个帖子有点问题我就重新发一个吧,下面的源码是我从今年3月份开始不断整理源码区和其他网站上的android源码,目前总共有720套左右,根据实现的功能被我分成了100多个类,总共 ...

  2. c#服务端图片打包下载

    一,设计多图片打包下载逻辑:1,如果是要拉取腾讯云等资源服务器的图片,2,我们先把远程图片拉取到本地的临时文件夹,3,然后压缩临时文件夹,4,压缩完删除临时文件夹,5,返回压缩完给用户,6,用户就去请 ...

  3. ASP.NET五步打包下载Zip文件

    本文版权归博客园和作者吴双共同所有,转载和爬虫请注明原文地址:www.cnblogs.com/tdws 首先分享几个振奋人心的新闻: 1.谷歌已经宣布加入.NET基金会 2.微软加入Linux基金会, ...

  4. 射手网字幕打包下载(73.16G)

    射手网陪着我度过15年了. 我所希望射手网所具有的价值,就是能令更多人跨越国家的樊篱,了解世界上不同的文化. 如果这个网站有帮到人,我就已经很满足了. 但是,需要射手网的时代已经走开了. 因此,今天, ...

  5. ASP.NET 打包下载文件

    使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...

  6. C#.NET快速开发框架-企业版V4.0截图打包下载

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) http://www.csframework.com/cs-framework-4.0.htm 其它图片打包下载: ht ...

  7. ASP.NET多文件批量打包下载

    在对多文件打包中用到了 DotNetZip 的方法来实现对多文件压缩打包.需要到http://dotnetzip.codeplex.com/处下载该文件,然后引用即可. Default.aspx: & ...

  8. 开源 & 免费使用 & 打包下载自行部署 :升讯威 周报系统

    这个周报系统大约写于2015年,缘起当时所带的开发团队需要逐步建立或完善一些项目管理方法. 在调研了网上的诸多项目管理或周报/日报管理系统之后,并没有找到符合当时情况的系统,这里最大的问题不是网上既有 ...

  9. java 实现多文件打包下载

    jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...

随机推荐

  1. 高精度乘法模板(luogu1303)

    洛谷1303 //luogu1303,不压位的高精度乘法 #include <cstdio> #include <iostream> using namespace std; ...

  2. paper 166:梯度下降法及其Python实现

    参考来源:https://blog.csdn.net/yhao2014/article/details/51554910 梯度下降法(gradient descent),又名最速下降法(steepes ...

  3. post方式请求数据

    post方式请求数据 分析: 1.将请求方式改成post conn.setRequestMethod("POST"); 2.设置连接可以输出 conn.setDoOutput(tr ...

  4. Windows10下安装CentOS7双系统

    参考: 参考1 参考2 问题1

  5. Vue&webpack入门实践

    目录 1. 下载安装Vue 2. Vue 2.1 Vue要素 2.2 指令 2.3 组件化 2.4 vue-router 3. webpack 3.1 webpack简介 3.2 四个核心概念 3.3 ...

  6. delphi 在代码中 添加 TO-DO 并且 管理

    TO-DO List是一项非常好用的功能.采用她可以让我们很清楚的了解以前完成了那些任务,还有哪些任务需要做,由谁负责完成,是不是比较紧急的任务等.今天来不及完成的,明天上班就可以很快的找到任务所在的 ...

  7. 安装php时,configure: error: xml2-config not found. Please check your libxml2 installation

    参考文章:http://blog.csdn.net/anljf/article/details/6981247 安装php时的报错configure: error: xml2-config not f ...

  8. spring 注释

    4

  9. Html5 学习笔记 --》布局

    不推荐: 浮动布局: footer 设置 clear : both 清理浮动 |  header            |  |边 |      | |内    |            内容     ...

  10. 使用Bochs学习硬件原理

    什么是Bochs? 简单地说,Bochs是一款仿真软件,可以用软件的方式模拟硬件的工作.同类软件有Qemu,仿真软件与虚拟机(hypervisor)还不完全相同,仿真软件是完全软件模拟硬件,而虚拟机软 ...