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实现ping
Oct 09 PHP
PHP如何透过ODBC来存取数据库
Oct 09 PHP
php 分页函数multi() discuz
Jun 21 PHP
pdo中使用参数化查询sql
Aug 11 PHP
谨慎使用PHP的引用原因分析
Sep 06 PHP
PHP strstr 函数判断字符串是否否存在的实例代码
Sep 28 PHP
简单实用的.net DataTable导出Execl
Oct 28 PHP
php实现信用卡校验位算法THE LUHN MOD-10示例
May 07 PHP
PHP+jquery实时显示网站在线人数的方法
Jan 04 PHP
PHP使用socket发送HTTP请求的方法
Feb 14 PHP
PHP大文件切割上传功能实例分析
Jul 01 PHP
PHP使用PDO实现mysql防注入功能详解
Dec 20 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
怎样在UNIX系统下安装php3
2006/10/09 PHP
PHP Ajax实现页面无刷新发表评论
2007/01/02 PHP
windows下PHP APACHE MYSQ完整配置
2007/01/02 PHP
实现php加速的eAccelerator dll支持文件打包下载
2007/09/30 PHP
php数组函数序列之array_unique() - 去除数组中重复的元素值
2011/10/29 PHP
php原生导出excel文件的两种方法(推荐)
2016/11/19 PHP
PHP实现简单计算器小程序
2020/08/28 PHP
PHP模糊查询技术实例分析【附源码下载】
2019/03/07 PHP
Javascript 面向对象 重载
2010/05/13 Javascript
该如何加载google-analytics(或其他第三方)的JS
2010/05/13 Javascript
基于jQuery的淡入淡出可自动切换的幻灯插件
2010/08/24 Javascript
html页面显示年月日时分秒和星期几的两种方式
2013/08/20 Javascript
nodejs npm package.json中文文档
2014/09/04 NodeJs
javascript生成大小写字母
2015/07/03 Javascript
JS判断页面是否出现滚动条的方法
2015/07/17 Javascript
jQuery打字效果实现方法(附demo源码下载)
2015/12/18 Javascript
jquery正则表达式验证(手机号、身份证号、中文名称)
2015/12/31 Javascript
在web中js实现类似excel的表格控件
2016/09/01 Javascript
ionic2自定义cordova插件开发以及使用(Android)
2017/06/19 Javascript
js分页之前端代码实现和请求处理
2017/08/04 Javascript
gulp安装以及打包合并的方法教程
2017/11/19 Javascript
JavaScript字符串转数字的5种方法及遇到的坑
2018/07/16 Javascript
react 国际化的实现代码示例
2018/09/14 Javascript
使用webpack4编译并压缩ES6代码的方法示例
2019/04/24 Javascript
javascript实现弹出层效果
2019/12/10 Javascript
ES6函数实现排它两种写法解析
2020/05/13 Javascript
[20:21]《一刀刀一天》第十六期:TI国际邀请赛正式打响,总奖金超过550万
2014/05/23 DOTA
Python 3实战爬虫之爬取京东图书的图片详解
2017/10/09 Python
html5 canvas-2.用canvas制作一个猜字母的小游戏
2013/01/07 HTML / CSS
IBatis持久层技术
2016/07/18 面试题
专科毕业生求职简历的自我评价
2013/10/12 职场文书
化工机械应届生求职信
2013/11/04 职场文书
预备党员自我批评思想汇报
2014/10/10 职场文书
爱心募捐通知范文
2015/04/27 职场文书
2016道德模范先进事迹材料
2016/02/26 职场文书
CSS巧用渐变实现高级感背景光动画
2021/12/06 HTML / CSS