Heim > php教程 > php手册 > Hauptteil

PHP生成压缩文件开发实例

WBOY
Freigeben: 2016-06-06 19:43:37
Original
1356 Leute haben es durchsucht

大概需求: 每一个订单都有多个文件附件,在下载的时候希望对当前订单的文件自动打包成一个压缩包下载 细节需求:当前订单号_年月日+时间.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);
Nach dem Login kopieren

 

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>
Nach dem Login kopieren

 

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

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

 

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage