大概需求:

每一个订单都有多个文件附件,在下载的时候希望对当前订单的文件自动打包成一个压缩包下载

细节需求:当前订单号_年月日+时间.zip  例如:

1.生成压缩文件,压缩文件名格式:

2.压缩文件存放在根目录 /upload/zipfile/年月/自定义的压缩文件名.zip

3.点击下载压缩包,系统开始对压缩文件打包,打包完成后自动开始下载

4.为了防止暴露压缩包文件路径,需要对下载的压缩包文件名改名

具体操作模式请见下面的代码:

文件路径:

压缩包文件存放路径:/upload/zipfile/

上传的附件存放路径:/upload/file/

1.基本配置文件文件 config.inc.php放在系统根目录

  1. define('SYS_ROOT', str_replace("\\", '/', dirname(__FILE__)));
  2. define('SYS_UPLOAD', SYS_ROOT.'/upload/file');
  3. define('SYS_DOWNLOAD', SYS_ROOT.'/upload/zipfile');
  4. define('SYS_WIN', strpos(strtoupper(PHP_OS), 'WIN') !== false ? true: false);
  5. define('SYS_CHMOD', ('0777' && !SYS_WIN) ? '0777' : 0);

2.压缩包程序代码文件 getzip.php

  1. header("Content-type: text/html; charset=utf-8");
  2. require_once '../config.inc.php'; //载入配置路径配置文件
  3. $arrfiles = array(SYS_UPLOAD . '/1.jpg',
  4. SYS_UPLOAD . '/x.jpg',); //这里是附件的文件数组
  5. $orderNum = '888'; //订单号
  6. $downFileName = 'tieniu.zip'; //下载的文件名 如果为空那么就是系统自定义名称 如果指定就显示指定名字
  7. $zipUrl = create_zip($arrfiles, $orderNum); //生成的压缩文件名词
  8. file_down($zipUrl, $downFileName); //提供http下载,并可以进行重命名下载文件,建议重命名,防止路径猜解
  9.  
  10. /*
  11. * 生成压缩包文件名
  12. * @param [String] $orderNum 订单号
  13. * @return [String] 返回带有绝对路径的订单号的压缩文件名
  14. */
  15.  
  16. function get_zipname($orderNum) {
  17. $zipName = SYS_DOWNLOAD . '/' . date('Ym') . '/' . $orderNum . '_' . date("Ymd_Hi") . '.zip';
  18. return $zipName;
  19. }
  20.  
  21. /*
  22. * 按照特定需求打包压缩包的目录结构设置
  23. */
  24.  
  25. function pack_object() {
  26.  
  27. }
  28.  
  29. /*
  30. * 生成压缩包
  31. * @param [Array] $arrfiles 带有绝对路径的文件数组
  32. * @param [String] $orderNum 订单号
  33. * @return [String] 返回带有绝对路径的订单号的压缩文件名 如如果失败返回 FALSE
  34. */
  35.  
  36. function create_zip($arrfiles, $orderNum) {
  37. $zipName = get_zipname($orderNum); //获得文件名
  38. dir_create(dirname($zipName)); //建立生成压缩文件的目录
  39. $zip = new ZipArchive();
  40. if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
  41. return FALSE;
  42. }
  43. foreach ($arrfiles as $path) {
  44. if (is_file($path)) {//判断文件是否存在
  45. $zip->addFile($path, basename($path)); //把文件加入到压缩包中
  46. }
  47. }
  48. $zip->close();
  49. return $zipName;
  50. }
  51.  
  52. /*
  53. * 处理文件目录
  54. * @param [Array] $arrfiles 带有绝对路径的文件数组
  55. * @param [String] $dirpath 文件路径
  56. * @return [String] 返回处理的文件路径,方便生成文件目录
  57. */
  58.  
  59. function dir_path($dirpath) {
  60. $dirpath = str_replace('\\', '/', $dirpath);
  61. if (substr($dirpath, -1) != '/')
  62. $dirpath = $dirpath . '/';
  63. return $dirpath;
  64. }
  65.  
  66. /*
  67. * 生成文件目录
  68. * @param [String] $path 文件路径
  69. * @return [String] 返回生成的文件目录结构
  70. */
  71.  
  72. function dir_create($path) {
  73. if (is_dir($path))
  74. return true;
  75. $dir = str_replace(SYS_DOWNLOAD . '/', '', $path);
  76. $dir = dir_path($dir);
  77. $temp = explode('/', $dir);
  78. $cur_dir = SYS_DOWNLOAD . '/';
  79. $max = count($temp) - 1;
  80. for ($i = 0; $i < $max; $i++) {
  81. $cur_dir .= $temp[$i] . '/';
  82. if (is_dir($cur_dir))
  83. continue;
  84. @mkdir($cur_dir);
  85. if (SYS_CHMOD)
  86. @chmod($cur_dir, SYS_CHMOD);
  87. if (!is_file($cur_dir . '/index.html') && !is_file($cur_dir . '/index.php'))
  88. file_copy(SYS_ROOT . '/upload/index.html', $cur_dir . '/index.html');
  89. }
  90.  
  91. return is_dir($path);
  92. }
  93.  
  94. /*
  95. * 文件COPY
  96. * @param [String] $from copy源文件
  97. * @param [String] $to copy文件目的地
  98. * @return [Bool] 成功 ture 失败 false
  99. */
  100.  
  101. function file_copy($from, $to) {
  102. dir_create(dirname($to));
  103. if (is_file($to) && SYS_CHMOD)
  104. @chmod($to, SYS_CHMOD);
  105. if (@copy($from, $to)) {
  106. if (SYS_CHMOD)
  107. @chmod($to, SYS_CHMOD);
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113.  
  114. /*
  115. * 文件下载处理函数
  116. * @param [String] $file 文件路径
  117. * @param [String] $filename 下载时间重新命名的文件名
  118. * @param [String] $data 下载文件填装的数据内容
  119. */
  120.  
  121. function file_down($file, $filename = '', $data = '') {
  122. if (!$data && !is_file($file))
  123. exit;
  124. $filename = $filename ? $filename : basename($file);
  125. $filetype = file_ext($filename);
  126. $filesize = $data ? strlen($data) : filesize($file);
  127. ob_end_clean();
  128. @set_time_limit(0);
  129. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
  130. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  131. header('Pragma: public');
  132. } else {
  133. header('Pragma: no-cache');
  134. }
  135. header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  136. header('Content-Encoding: none');
  137. header('Content-Length: ' . $filesize);
  138. header('Content-Disposition: attachment; filename=' . $filename);
  139. header('Content-Type: ' . $filetype);
  140. if ($data) {
  141. echo $data;
  142. } else {
  143. readfile($file);
  144. }
  145. exit;
  146. }
  147.  
  148. function file_ext($filename) {
  149. return strtolower(trim(substr(strrchr($filename, '.'), 1)));
  150. }
  151.  
  152. //此函数未用到,用来做整个目录的打包下载
  153. function listdir($start_dir = '.') {
  154. $files = array();
  155. if (is_dir($start_dir)) {
  156. $fh = opendir($start_dir);
  157. while (($file = readdir($fh)) !== false) {
  158. if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0)
  159. continue;
  160. $filepath = $start_dir . '/' . $file;
  161. if (is_dir($filepath))
  162. $files = array_merge($files, listdir($filepath));
  163. else
  164. array_push($files, $filepath);
  165. }
  166. closedir($fh);
  167. } else {
  168. $files = false;
  169. }
  170. return $files;
  171. }

3.PHP程序生成压缩文件需要用到压缩类:ZipArchive

这个是php的扩展类,自php5.2版本以后就已经支持这个扩展,如果你在使用的时候出现错误,查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉,然后再重启Apache这样才能使用这个类库。

程序下载地址:http://download.csdn.net/detail/venus150/8799855

欢迎加入PHP技术和职场交流群:383730534

PHP生成压缩文件开发实例的更多相关文章

  1. java生成压缩文件

    在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载.所以自己写了一个压缩文件的工具类.该工具类支持单个文件和文件夹压缩.放代码: import java.io.BufferedOutput ...

  2. ASP.NET生成压缩文件(rar打包)

    首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/files/cang12138/ICSharpCode.SharpZi ...

  3. Java生成压缩文件(zip、rar 格式)

    jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...

  4. css javascript 自动化压缩(保存后即自动生成压缩文件)

    先上图:

  5. C#利用SharpZipLib解压或压缩文件夹实例操作

    最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享. 这里主要解决文件夹包含文件夹的解压缩问题. )下载SharpZipLib.dll,在http://www.icsharpcode. ...

  6. php 3.2 生成压缩文件,并下载

    public function zip_download() { $array = array( 'http://local.qki.com/site_upload/erweima/20190826/ ...

  7. php 生成压缩文件

    $fileList = array( "site_upload/form_file_clause_extend/20180224/1519456901_1481718257.jpg" ...

  8. C#压缩文件 不压缩路径

    我想把 E:\\AA\BB\11.txt 压缩后存入 E:\\AA1\BB1\11.rar 但是当我解压( E:\\AA1\BB1\11.rar)的时候,发现:11.txt 不是在 E:\\AA1\B ...

  9. C#压缩文件为zip格式

    Vercher   C#压缩文件为zip格式 需要ICSharpCode.SharpZipLib.dll,网上下载的到. 代码是从网上找来的: 1 public class ZipClass 2 { ...

随机推荐

  1. JavaScript中Get和Set访问器的实现

    我们常用的实现方法可能是这样的: function Field(val){ var value = val; this.getValue =function(){ return value; }; t ...

  2. PHP 环境塔建与数据类型转换

    手动塔建PHP开发环境 安装php c:\apps\php 安装apache c:\apps\apache 1.配制apache 配制c:\apps\apache\conf\httpd.conf Do ...

  3. wcf自身作为宿主的一个小案例

    第一步:创建整个解决方案 service.interface:用于定义服务的契约(所有的类的接口)引用了wcf的核心程序集system.ServiceModel.dll service:用于定义服务类 ...

  4. android startActivityForResult(Intent intent, int requestCode) 整理与总结! .

    假设有两个Activity,主界面A,功能界面B,由A启动B,并传数据给B,B在经过处理后把数据传回给A. 先是A传B: Bundle bundle = new Bundle();bundle.put ...

  5. springMVC项目在jboss7中配置应用自己的log4j--转载

    原文地址:http://www.xuebuyuan.com/1954635.html Jboss7默认采用容器自己的log4j module,应用自己配置的log4j不起作用,需要应用做一些设置: 以 ...

  6. Middleware

    Middleware The middleware gives a single shot to the views associated into Controllers, before execu ...

  7. js重写原型对象

    首先看两段很相似的代码: 1. function Person(){} Person.prototype = { constructor:Person, name:"Nic", a ...

  8. windows下mysql初始密码设置

    转载自:http://blog.csdn.net/ofreelander/article/details/50802780 1.my-default.ini 改名my.ini 在解压的目录下面复制my ...

  9. Intent传值之通过Application传值

    传值第五种方式: * 程序的全局变量application * 特点:1.一个程序application对象只能有一个 * 2.application对象在程序启动时就创建 * 3.通常用来存放全局变 ...

  10. compile ffmpeg

    download SDL 1.2.xxx version source code. 1) configure 2) make & make instll download recent ffm ...