php简单创建zip压缩文件的方法


Posted in PHP onApril 30, 2016

本文实例讲述了php简单创建zip压缩文件的方法。分享给大家供大家参考,具体如下:

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {
    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
      if(file_exists($file)) {
        $valid_files[] = $file;
      }
    }
  }
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
      return false;
    }
    //add the files
    foreach($valid_files as $file) {
      $zip->addFile($file,$file);
    }
    //debug
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
    //close the zip -- done!
    $zip->close();
    //check to make sure the file exists
    return file_exists($destination);
  }
  else
  {
    return false;
  }
}

使用方法:

$files_to_zip = array(
  'preload-images/1.jpg',
  'preload-images/2.jpg',
  'preload-images/5.jpg',
  'kwicks/ringo.gif',
  'rod.jpg',
  'reddit.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
NOD32 v2.70.32 简体中文封装版 提供下载了
Feb 27 PHP
php为什么选mysql作为数据库? Mysql 创建用户方法
Jul 02 PHP
php 读取文件乱码问题
Feb 20 PHP
PHP header函数分析详解
Aug 06 PHP
PHP+Mysql+jQuery实现动态展示信息
Oct 08 PHP
php设计模式小结
Feb 15 PHP
学习php中的正则表达式
Aug 17 PHP
PHP实现恶意DDOS攻击避免带宽占用问题方法
May 27 PHP
mac os快速切换多个PHP版本的方法
Mar 07 PHP
PHP设计模式之单例模式原理与实现方法分析
Apr 25 PHP
ThinkPHP5.0框架控制器继承基类和自定义类示例
May 25 PHP
Thinkphp5框架实现获取数据库数据到视图的方法
Aug 14 PHP
Yii2 rbac权限控制操作步骤实例教程
Apr 29 #PHP
PHP.vs.JAVA
Apr 29 #PHP
Yii实现简单分页的方法
Apr 29 #PHP
php实现在站点里面添加邮件发送的功能
Apr 28 #PHP
php提交过来的数据生成为txt文件
Apr 28 #PHP
php生成txt文件实例代码介绍
Apr 28 #PHP
100行PHP代码实现socks5代理服务器
Apr 28 #PHP
You might like
$_GET['goods_id']+0 的使用详解
2013/06/06 PHP
深入PHP内存相关的功能特性详解
2013/06/08 PHP
PHP 函数call_user_func和call_user_func_array用法详解
2014/03/02 PHP
PHP实现发送邮件的方法(基于简单邮件发送类)
2015/12/17 PHP
PHP删除数组中特定元素的两种方法
2019/02/28 PHP
yii2 开发api接口时优雅的处理全局异常的方法
2019/05/14 PHP
PHP ob缓存以及ob函数原理实例解析
2020/11/13 PHP
Javascript代码混淆综合解决方案-Javascript在线混淆器
2006/12/18 Javascript
始终在屏幕中间显示Div的代码(css+js)
2011/03/10 Javascript
Js 获取Gridview选中行的内容操作步骤
2013/02/05 Javascript
用客户端js实现带省略号的分页
2013/04/27 Javascript
javascript中select下拉框的用法总结
2016/01/07 Javascript
jquery实用技巧之输入框提示语句
2016/07/28 Javascript
Angular 中 select指令用法详解
2016/09/29 Javascript
AngularJS实现在ng-Options加上index的解决方法
2016/11/03 Javascript
webpack踩坑之路图片的路径与打包
2017/09/05 Javascript
JS中的算法与数据结构之字典(Dictionary)实例详解
2019/08/20 Javascript
JQuery复选框全选效果如何实现
2020/05/08 jQuery
python3之微信文章爬虫实例讲解
2017/07/12 Python
mac安装pytorch及系统的numpy更新方法
2018/07/26 Python
Python设计模式之解释器模式原理与用法实例分析
2019/01/10 Python
用Python实现大文本文件切割的方法
2019/01/12 Python
Python math库 ln(x)运算的实现及原理
2019/07/17 Python
flask框架url与重定向操作实例详解
2020/01/25 Python
Python实现AI自动抠图实例解析
2020/03/05 Python
浅谈django 重载str 方法
2020/05/19 Python
python 调整图片亮度的示例
2020/12/03 Python
CSS中几个与换行有关的属性简明总结
2014/04/15 HTML / CSS
墨西哥运动服饰和鞋网上商店:Netshoes墨西哥
2016/07/28 全球购物
法国综合购物网站:RueDuCommerce
2016/09/12 全球购物
美国著名手表网站:Timepiece
2017/11/15 全球购物
2014年团队工作总结
2014/11/24 职场文书
一年级数学下册复习计划
2015/01/17 职场文书
2015年度环卫处工作总结
2015/07/24 职场文书
品德与社会教学反思
2016/02/24 职场文书
创业计划书之网络外卖
2019/10/31 职场文书