php将文件夹打包成zip文件的简单实现方法

yipeiwu_com6年前PHP代码库

示例如下:

function addFileToZip($path,$zip){
  $handler=opendir($path); //打开当前文件夹由$path指定。
  while(($filename=readdir($handler))!==false){
    if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..',不要对他们进行操作
      if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
        addFileToZip($path."/".$filename, $zip);
      }else{ //将文件加入zip对象
        $zip->addFile($path."/".$filename);
      }
    }
  }
  @closedir($path);
}


$zip=new ZipArchive();
if($zip->open('images.zip', ZipArchive::OVERWRITE)=== TRUE){
  addFileToZip('images/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
  $zip->close(); //关闭处理的zip文件
}

以上就是小编为大家带来的php将文件夹打包成zip文件的简单实现方法全部内容了,希望大家多多支持【宜配屋www.yipeiwu.com】~

相关文章

php select,radio和checkbox默认选择的实现方法

这是扩展yibing的select默认选择的实现方法 复制代码 代码如下: <select name="wuyeleixing" size="1"> <option &...

PHP中COOKIES使用示例

主要是在登录和退出的时候,设置cookies。来保存登录和安全退出 1:在登录页面设置 //设置cookies的值 _setcookies($_rows['tg_username']...

火车头discuz6.1 完美采集的php接口文件

PS:对原文件的修改较大,程序中注释已经很详尽,这里就不多说了。 复制代码 代码如下:<?php // header('Content-Type:text/html;charset...

解析PHP中DIRECTORY_SEPARATOR,PATH_SEPARATOR两个常量的作用

一个是:DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR:路径分隔符,linux上就是‘/'    windows上是‘\'...

php set_magic_quotes_runtime() 函数过时解决方法

把函数: set_magic_quotes_runtime($new_setting); 替换成: ini_set("magic_quotes_runtime", $new_settin...