php多张图片打包下载
<?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();
//打包下载实例操作过程
- <?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();
- //打包下载实例操作过程
php多张图片打包下载的更多相关文章
- 2014年最新720多套Android源码2.0GB免费一次性打包下载
之前发过一个帖子,但是那个帖子有点问题我就重新发一个吧,下面的源码是我从今年3月份开始不断整理源码区和其他网站上的android源码,目前总共有720套左右,根据实现的功能被我分成了100多个类,总共 ...
- c#服务端图片打包下载
一,设计多图片打包下载逻辑:1,如果是要拉取腾讯云等资源服务器的图片,2,我们先把远程图片拉取到本地的临时文件夹,3,然后压缩临时文件夹,4,压缩完删除临时文件夹,5,返回压缩完给用户,6,用户就去请 ...
- ASP.NET五步打包下载Zip文件
本文版权归博客园和作者吴双共同所有,转载和爬虫请注明原文地址:www.cnblogs.com/tdws 首先分享几个振奋人心的新闻: 1.谷歌已经宣布加入.NET基金会 2.微软加入Linux基金会, ...
- 射手网字幕打包下载(73.16G)
射手网陪着我度过15年了. 我所希望射手网所具有的价值,就是能令更多人跨越国家的樊篱,了解世界上不同的文化. 如果这个网站有帮到人,我就已经很满足了. 但是,需要射手网的时代已经走开了. 因此,今天, ...
- ASP.NET 打包下载文件
使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...
- C#.NET快速开发框架-企业版V4.0截图打包下载
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) http://www.csframework.com/cs-framework-4.0.htm 其它图片打包下载: ht ...
- ASP.NET多文件批量打包下载
在对多文件打包中用到了 DotNetZip 的方法来实现对多文件压缩打包.需要到http://dotnetzip.codeplex.com/处下载该文件,然后引用即可. Default.aspx: & ...
- 开源 & 免费使用 & 打包下载自行部署 :升讯威 周报系统
这个周报系统大约写于2015年,缘起当时所带的开发团队需要逐步建立或完善一些项目管理方法. 在调研了网上的诸多项目管理或周报/日报管理系统之后,并没有找到符合当时情况的系统,这里最大的问题不是网上既有 ...
- java 实现多文件打包下载
jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...
随机推荐
- Python基础教程(018)--官方解释器交互运行
前言: 在交互式运行Python程序 内容 在Python的shell中直接输入Python的代码,可以立即执行结果 交互式运行Python的优缺点 1,缺点--代码不能保存 2,不适合运行太大的程序 ...
- 怎么更改win7登录界面
方法/步骤 1 第一步,先打开注册表.快捷键是win+R.Win就是Windows图片那个键.打开会是这个. 2 在其中输入Regedit.就打开了传说中的注册表了.然后在注册表中选择.选择的顺序 ...
- [CSU1806]Toll
题目:Toll 传送门:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1806 题目简述:给定n个点m条有向边的有向图,每条边的花费是$b_i ...
- 测开之路三十五:css引入
CSS是一种定义样式结构,如字体.颜色.位置等的语言,被用于描述网页上的信息格式化和现实的方式.CSS样式可以直接存储于HTML网页或者单独的样式单文件.无论哪一种方式,样式单包含将样式应用到指定类型 ...
- flysql 里两种传参的方式
传参的方式,两个标清楚: for lists_bx_goods in out_list: sql = XDO().get_update_sql('init_goods_test', { "一 ...
- Java集合的介绍
参考博客: https://blog.csdn.net/zhangqunshuai/article/details/80660974 List , Set, Map都是接口,前两个继承至Collect ...
- QTP使用dictionary 对象
1. 创建即使用Dictionary对象 ' 创建Dictionary对象Set Dic = CreateObject("Scripting.Dictionary")' 添加Dic ...
- Advanved DataGrid using QTP
Use the GetCellData(j,i) Function for Cell data and Use the GetRowData(j) Function for Row Data wher ...
- Cocos2d-x之String
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Cocos2d-x中能够使用的字符串constchar*.std::string和cocos2d::__String等,其中const ...
- JNDI数据源的配置及使用
数据源的作用JDBC操作的步骤: 1. 加载驱动程序 2. 连接数据库 3. 操作数据库 4. 关闭数据库,释放连接 也就是说,所有的用户都需要经过此四步进行操作,但是这四步之中有三步对所有人 ...