background image

PHP 功能实现:PHP 生成压缩 ZIP 文件代码

php 压缩文件 zip

 

的例子

1.请先下载我准备好的 zip.php 工具类,下载后解压,将里面的文件放入对应的目录中,
我是放在虚拟目录下的 include 文件夹中。
2.在你的 php 文件中加入下面代码即可
用法:
require_once “./include/zip.php”;
$zip = new PHPZip();
//$zip -> createZip(“

要压缩的文件夹目录地址 , “压缩后的文件名.zip”);

 

   //只生成

不自动下载
$zip -> downloadZip(“

要压缩的文件夹目录地址 , “压缩后的文件名.zip”); //自动下

 

实例:可以参考下面的伪代码来看一个具体的使用场景: 代码
require_once “./include/zip.php”;
if (!$download) {
exit();
}
set_time_limit(60);
$tmpManager = new TmpManager(); //假设我们有一个类来完成后面的操作
$tempfolder = array();
$tempfile = array(); //假设我们是通过在页面上选择 checkbox 来下载已选的文件夹
或文件,并一同打包
for($i = 0;$i < $checkboxnum;$i ) {
$value = ${“select”.$i};
if ($value != ”) {
$this_type = substr($value, 0, 1);
$this_id = substr($value, 1);
//将文件夹和文件的情况分开处理
if ($this_type == ‘d’) {
$tempfolder[] = $this_id;
}
elseif ($this_type == ‘f’) {
$tempfile[] = $this_id;
}
}
}
@mkdir($tempdir);
$curtempdir = “$tempdir/”.$userid; //不同用户在不同的临时文件夹下操作