首页 php教程 php手册 PHP生成压缩文件开发实例

PHP生成压缩文件开发实例

Jun 06, 2016 pm 07:43 PM
php 压缩 开发实例 文件 生成 订单 需求

大概需求: 每一个订单都有多个文件附件,在下载的时候希望对当前订单的文件自动打包成一个压缩包下载 细节需求:当前订单号_年月日+时间.zip 例如: 1.生成压缩文件,压缩文件名格式: 2.压缩文件存放在根目录 /upload/zipfile/年月/自定义的压缩文件名.zip

大概需求:

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

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

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

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

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

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

 

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

文件路径:

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

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

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

 

<span>define</span>('SYS_ROOT', <span>str_replace</span>("\\", '/', <span>dirname</span>(<span>__FILE__</span><span>)));
</span><span>define</span>('SYS_UPLOAD', SYS_ROOT.'/upload/file'<span>);
</span><span>define</span>('SYS_DOWNLOAD', SYS_ROOT.'/upload/zipfile'<span>);
</span><span>define</span>('SYS_WIN', <span>strpos</span>(<span>strtoupper</span>(<span>PHP_OS</span>), 'WIN') !== <span>false</span> ? <span>true</span>: <span>false</span><span>);
</span><span>define</span>('SYS_CHMOD', ('0777' && !SYS_WIN) ? '0777' : 0);
登录后复制

 

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

<span>header</span>("Content-type: text/html; charset=utf-8"<span>);
</span><span>require_once</span> '../config.inc.php'; <span>//</span><span>载入配置路径配置文件</span>
<span>$arrfiles</span> = <span>array</span>(SYS_UPLOAD . '/1.jpg',<span>
    SYS_UPLOAD </span>. '/x.jpg',); <span>//</span><span>这里是附件的文件数组</span>
<span>$orderNum</span> = '888'; <span>//</span><span>订单号</span>
<span>$downFileName</span> = 'tieniu.zip'; <span>//</span><span>下载的文件名 如果为空那么就是系统自定义名称 如果指定就显示指定名字</span>
<span>$zipUrl</span> = create_zip(<span>$arrfiles</span>, <span>$orderNum</span>); <span>//</span><span>生成的压缩文件名词</span>
file_down(<span>$zipUrl</span>, <span>$downFileName</span>); <span>//</span><span>提供http下载,并可以进行重命名下载文件,建议重命名,防止路径猜解</span>

<span>/*</span><span>
 * 生成压缩包文件名
 * @param  [String] $orderNum 订单号
 * @return [String] 返回带有绝对路径的订单号的压缩文件名
 </span><span>*/</span>

<span>function</span> get_zipname(<span>$orderNum</span><span>) {
    </span><span>$zipName</span> = SYS_DOWNLOAD . '/' . <span>date</span>('Ym') . '/' . <span>$orderNum</span> . '_' . <span>date</span>("Ymd_Hi") . '.zip'<span>;
    </span><span>return</span> <span>$zipName</span><span>;
}

</span><span>/*</span><span>
 * 按照特定需求打包压缩包的目录结构设置
 </span><span>*/</span>

<span>function</span><span> pack_object() {
    
}

</span><span>/*</span><span>
 * 生成压缩包
 * @param  [Array] $arrfiles 带有绝对路径的文件数组
 * @param  [String] $orderNum 订单号
 * @return [String] 返回带有绝对路径的订单号的压缩文件名 如如果失败返回 FALSE
 </span><span>*/</span>

<span>function</span> create_zip(<span>$arrfiles</span>, <span>$orderNum</span><span>) {
    </span><span>$zipName</span> = get_zipname(<span>$orderNum</span>); <span>//</span><span>获得文件名</span>
    dir_create(<span>dirname</span>(<span>$zipName</span>)); <span>//</span><span>建立生成压缩文件的目录</span>
    <span>$zip</span> = <span>new</span><span> ZipArchive();
    </span><span>if</span> (<span>$zip</span>->open(<span>$zipName</span>, ZIPARCHIVE::CREATE) !== <span>TRUE</span><span>) {
        </span><span>return</span> <span>FALSE</span><span>;
    }
    </span><span>foreach</span> (<span>$arrfiles</span> <span>as</span> <span>$path</span><span>) {
        </span><span>if</span> (<span>is_file</span>(<span>$path</span>)) {<span>//</span><span>判断文件是否存在</span>
            <span>$zip</span>->addFile(<span>$path</span>, <span>basename</span>(<span>$path</span>)); <span>//</span><span>把文件加入到压缩包中</span>
<span>        }
    }
    </span><span>$zip</span>-><span>close();
    </span><span>return</span> <span>$zipName</span><span>;
}

</span><span>/*</span><span>
 * 处理文件目录
 * @param  [Array] $arrfiles 带有绝对路径的文件数组
 * @param  [String] $dirpath 文件路径
 * @return [String] 返回处理的文件路径,方便生成文件目录
 </span><span>*/</span>

<span>function</span> dir_path(<span>$dirpath</span><span>) {
    </span><span>$dirpath</span> = <span>str_replace</span>('\\', '/', <span>$dirpath</span><span>);
    </span><span>if</span> (<span>substr</span>(<span>$dirpath</span>, -1) != '/'<span>)
        </span><span>$dirpath</span> = <span>$dirpath</span> . '/'<span>;
    </span><span>return</span> <span>$dirpath</span><span>;
}

</span><span>/*</span><span>
 * 生成文件目录
 * @param  [String] $path 文件路径
 * @return [String] 返回生成的文件目录结构
 </span><span>*/</span>

<span>function</span> dir_create(<span>$path</span><span>) {
    </span><span>if</span> (<span>is_dir</span>(<span>$path</span><span>))
        </span><span>return</span> <span>true</span><span>;
    </span><span>$dir</span> = <span>str_replace</span>(SYS_DOWNLOAD . '/', '', <span>$path</span><span>);
    </span><span>$dir</span> = dir_path(<span>$dir</span><span>);
    </span><span>$temp</span> = <span>explode</span>('/', <span>$dir</span><span>);
    </span><span>$cur_dir</span> = SYS_DOWNLOAD . '/'<span>;
    </span><span>$max</span> = <span>count</span>(<span>$temp</span>) - 1<span>;
    </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> $max; <span>$i</span>++<span>) {
        </span><span>$cur_dir</span> .= <span>$temp</span>[<span>$i</span>] . '/'<span>;
        </span><span>if</span> (<span>is_dir</span>(<span>$cur_dir</span><span>))
            </span><span>continue</span><span>;
        @</span><span>mkdir</span>(<span>$cur_dir</span><span>);
        </span><span>if</span><span> (SYS_CHMOD)
            @</span><span>chmod</span>(<span>$cur_dir</span>,<span> SYS_CHMOD);
        </span><span>if</span> (!<span>is_file</span>(<span>$cur_dir</span> . '/index.html') && !<span>is_file</span>(<span>$cur_dir</span> . '/index.php'<span>))
            file_copy(SYS_ROOT </span>. '/upload/index.html', <span>$cur_dir</span> . '/index.html'<span>);
    }

    </span><span>return</span> <span>is_dir</span>(<span>$path</span><span>);
}

</span><span>/*</span><span>
 * 文件COPY
 * @param  [String] $from copy源文件
 * @param  [String] $to copy文件目的地
 * @return [Bool]  成功 ture 失败 false
 </span><span>*/</span>

<span>function</span> file_copy(<span>$from</span>, <span>$to</span><span>) {
    dir_create(</span><span>dirname</span>(<span>$to</span><span>));
    </span><span>if</span> (<span>is_file</span>(<span>$to</span>) &&<span> SYS_CHMOD)
        @</span><span>chmod</span>(<span>$to</span>,<span> SYS_CHMOD);
    </span><span>if</span> (@<span>copy</span>(<span>$from</span>, <span>$to</span><span>)) {
        </span><span>if</span><span> (SYS_CHMOD)
            @</span><span>chmod</span>(<span>$to</span>,<span> SYS_CHMOD);
        </span><span>return</span> <span>true</span><span>;
    } </span><span>else</span><span> {
        </span><span>return</span> <span>false</span><span>;
    }
}

</span><span>/*</span><span>
 * 文件下载处理函数
 * @param  [String] $file 文件路径
 * @param  [String] $filename 下载时间重新命名的文件名
 * @param  [String] $data 下载文件填装的数据内容
 </span><span>*/</span>

<span>function</span> file_down(<span>$file</span>, <span>$filename</span> = '', <span>$data</span> = ''<span>) {
    </span><span>if</span> (!<span>$data</span> && !<span>is_file</span>(<span>$file</span><span>))
        </span><span>exit</span><span>;
    </span><span>$filename</span> = <span>$filename</span> ? <span>$filename</span> : <span>basename</span>(<span>$file</span><span>);
    </span><span>$filetype</span> = file_ext(<span>$filename</span><span>);
    </span><span>$filesize</span> = <span>$data</span> ? <span>strlen</span>(<span>$data</span>) : <span>filesize</span>(<span>$file</span><span>);
    </span><span>ob_end_clean</span><span>();
    @</span><span>set_time_limit</span>(0<span>);
    </span><span>if</span> (<span>strpos</span>(<span>$_SERVER</span>['HTTP_USER_AGENT'], 'MSIE') !== <span>false</span><span>) {
        </span><span>header</span>('Cache-Control: must-revalidate, post-check=0, pre-check=0'<span>);
        </span><span>header</span>('Pragma: public'<span>);
    } </span><span>else</span><span> {
        </span><span>header</span>('Pragma: no-cache'<span>);
    }
    </span><span>header</span>('Expires: ' . <span>gmdate</span>('D, d M Y H:i:s') . ' GMT'<span>);
    </span><span>header</span>('Content-Encoding: none'<span>);
    </span><span>header</span>('Content-Length: ' . <span>$filesize</span><span>);
    </span><span>header</span>('Content-Disposition: attachment; filename=' . <span>$filename</span><span>);
    </span><span>header</span>('Content-Type: ' . <span>$filetype</span><span>);
    </span><span>if</span> (<span>$data</span><span>) {
        </span><span>echo</span> <span>$data</span><span>;
    } </span><span>else</span><span> {
        </span><span>readfile</span>(<span>$file</span><span>);
    }
    </span><span>exit</span><span>;
}

</span><span>function</span> file_ext(<span>$filename</span><span>) {
    </span><span>return</span> <span>strtolower</span>(<span>trim</span>(<span>substr</span>(<span>strrchr</span>(<span>$filename</span>, '.'), 1<span>)));
}

</span><span>//</span><span>此函数未用到,用来做整个目录的打包下载</span>
<span>function</span> listdir(<span>$start_dir</span> = '.'<span>) {
    </span><span>$files</span> = <span>array</span><span>();
    </span><span>if</span> (<span>is_dir</span>(<span>$start_dir</span><span>)) {
        </span><span>$fh</span> = <span>opendir</span>(<span>$start_dir</span><span>);
        </span><span>while</span> ((<span>$file</span> = <span>readdir</span>(<span>$fh</span>)) !== <span>false</span><span>) {
            </span><span>if</span> (<span>strcmp</span>(<span>$file</span>, '.') == 0 || <span>strcmp</span>(<span>$file</span>, '..') == 0<span>)
                </span><span>continue</span><span>;
            </span><span>$filepath</span> = <span>$start_dir</span> . '/' . <span>$file</span><span>;
            </span><span>if</span> (<span>is_dir</span>(<span>$filepath</span><span>))
                </span><span>$files</span> = <span>array_merge</span>(<span>$files</span>, listdir(<span>$filepath</span><span>));
            </span><span>else</span>
                <span>array_push</span>(<span>$files</span>, <span>$filepath</span><span>);
        }
        </span><span>closedir</span>(<span>$fh</span><span>);
    } </span><span>else</span><span> {
        </span><span>$files</span> = <span>false</span><span>;
    }
    </span><span>return</span> <span>$files</span><span>;
}</span>
登录后复制

 

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

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

 

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

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 使用数据库 CakePHP 使用数据库 Sep 10, 2024 pm 05:25 PM

在 CakePHP 中使用数据库非常容易。本章我们将了解CRUD(创建、读取、更新、删除)操作。

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles