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如何编写易读的代码
Jul 10 PHP
Discuz 模板引擎的封装类代码
Jul 18 PHP
使用php批量删除数据库下所有前缀为prefix_的表
Jun 09 PHP
php通过sort()函数给数组排序的方法
Mar 18 PHP
PHP常见错误提示含义解释(实用!值得收藏)
Apr 25 PHP
php中static 静态变量和普通变量的区别
Dec 01 PHP
PHP对象实例化单例方法
Jan 19 PHP
PHP 7.1中AES加解密方法mcrypt_module_open()的替换方案
Oct 17 PHP
php工具型代码之印章抠图
Jul 18 PHP
PHP命名空间简单用法示例
Dec 28 PHP
Yii2框架操作数据库的方法分析【以mysql为例】
May 27 PHP
php+laravel 扫码二维码签到功能
May 15 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与MYSQL中UTF8 中文排序示例代码
2014/10/23 PHP
ThinkPHP做文字水印时提示call an undefined function exif_imagetype()解决方法
2014/10/30 PHP
PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
2017/12/21 PHP
PHP的RSA加密解密方法以及开发接口使用
2018/02/11 PHP
用js遍历 table的脚本
2008/07/23 Javascript
IE事件对象(The Internet Explorer Event Object)
2012/06/27 Javascript
canvas快速绘制圆形、三角形、矩形、多边形方法介绍
2016/12/29 Javascript
浅谈JavaScript中的apply/call/bind和this的使用
2017/02/26 Javascript
JavaScript函数表达式详解及实例
2017/05/05 Javascript
js禁止表单重复提交
2017/08/29 Javascript
浅谈在koa2中实现页面渲染的全局数据
2017/10/09 Javascript
深入理解JavaScript的值传递和引用传递
2018/10/24 Javascript
nodejs中各种加密算法的实现详解
2019/07/11 NodeJs
微信小程序自定义头部导航栏和导航栏背景图片 navigationStyle问题
2019/07/26 Javascript
layuiAdmin循环遍历展示商品图片列表的方法
2019/09/16 Javascript
vue-cli2与vue-cli3在一台电脑共存的实现方法
2019/09/25 Javascript
Vant Weapp组件踩坑:picker的初始赋值解决
2020/11/12 Javascript
Python向日志输出中添加上下文信息
2017/05/24 Python
django 单表操作实例详解
2019/07/30 Python
python twilio模块实现发送手机短信功能
2019/08/02 Python
基于Django框架的权限组件rbac实例讲解
2019/08/31 Python
Python for循环通过序列索引迭代过程解析
2020/02/07 Python
python 穷举指定长度的密码例子
2020/04/02 Python
HTML5中drawImage用法分析
2014/12/01 HTML / CSS
Servlet的生命周期
2013/08/25 面试题
销售工作岗位职责
2013/12/24 职场文书
春季运动会广播稿大全
2014/02/19 职场文书
校优秀毕业生主要事迹
2014/05/26 职场文书
建筑工地宣传标语
2014/06/18 职场文书
机械专业毕业生自我鉴定2014
2014/10/04 职场文书
平遥古城导游词
2015/02/03 职场文书
消夏晚会主持词
2015/06/30 职场文书
2016年班主任培训心得体会
2016/01/07 职场文书
创业计划书之情侣餐厅
2019/09/29 职场文书
MySQL 数据丢失排查案例
2021/05/08 MySQL
基于Redis结合SpringBoot的秒杀案例详解
2021/10/05 Redis