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 相关文章推荐
php.ini中文版
Oct 09 PHP
PHP常用函数小技巧
Sep 11 PHP
PHP安全的URL字符串base64编码和解码
Jun 19 PHP
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
Jun 23 PHP
PHP对象相互引用的内存溢出实例分析
Aug 28 PHP
php基于表单密码验证与HTTP验证用法实例
Jan 06 PHP
PHP使用fopen与file_get_contents读取文件实例分享
Mar 04 PHP
PHP实现将MySQL重复ID二维数组重组为三维数组的方法
Aug 01 PHP
php分页查询mysql结果的base64处理方法示例
May 18 PHP
PHP实现基于栈的后缀表达式求值功能
Nov 10 PHP
PHP PDOStatement::debugDumpParams讲解
Jan 30 PHP
Thinkphp极验滑动验证码实现步骤解析
Nov 24 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
php正则表达匹配中文问题分析小结
2012/03/25 PHP
php中文字符串截取方法实例总结
2014/09/30 PHP
ThinkPHP使用getlist方法实现数据搜索功能示例
2017/05/08 PHP
PHP聚合式迭代器接口IteratorAggregate用法分析
2017/12/28 PHP
PHP架构及原理知识点详解
2019/12/22 PHP
node.js入门教程迷你书、node.js入门web应用开发完全示例
2014/04/06 Javascript
Windows 系统下安装和部署Egret的开发环境
2014/07/31 Javascript
jQuery中prependTo()方法用法实例
2015/01/08 Javascript
javascript实现点击提交按钮后显示loading的方法
2015/07/03 Javascript
js实现兼容PC端和移动端滑块拖动选择数字效果
2017/02/16 Javascript
js仿新浪微博消息发布功能
2017/02/17 Javascript
Node.js编写CLI的实例详解
2017/05/17 Javascript
js轮播图无缝滚动效果
2017/06/17 Javascript
nodejs使用redis作为缓存介质实现的封装缓存类示例
2018/02/07 NodeJs
关于Vue Router中路由守卫的应用及在全局导航守卫中检查元字段的方法
2018/12/09 Javascript
微信小程序实现的picker多级联动功能示例
2019/05/23 Javascript
深入了解响应式React Native Echarts组件
2019/05/29 Javascript
react 生命周期实例分析
2020/05/18 Javascript
Vue如何跨组件传递Slot的实现
2020/12/14 Vue.js
angular *Ngif else用法详解
2020/12/15 Javascript
[05:31]干嘛呢兄弟!DOTA2 TI9语音轮盘部分出处
2019/05/14 DOTA
python实现通过shelve修改对象实例
2014/09/26 Python
python制作最美应用的爬虫
2015/10/28 Python
Python向MySQL批量插数据的实例讲解
2018/03/31 Python
Python3.5面向对象编程图文与实例详解
2019/04/24 Python
Pycharm简单使用教程(入门小结)
2019/07/04 Python
python实现最大子序和(分治+动态规划)
2019/07/05 Python
Python之——生成动态路由轨迹图的实例
2019/11/22 Python
详解Python多线程下的list
2020/07/03 Python
孤独星球出版物:Lonely Planet Publications
2018/03/17 全球购物
瑞士最大的图书贸易公司:Orell Füssli
2019/12/28 全球购物
爱尔兰旅游网站:ebookers.ie
2020/01/24 全球购物
普通PHP程序员笔试题
2016/01/01 面试题
水电维修专业推荐信
2014/09/06 职场文书
Python pandas读取CSV文件的注意事项(适合新手)
2021/06/20 Python
Oracle 临时表空间SQL语句的实现
2021/09/25 Oracle