php生成zip文件类实例


Posted in PHP onApril 07, 2015

本文实例讲述了php生成zip文件类。分享给大家供大家参考。具体如下:

<?php
 /*
  By:   Matt Ford
  Purpose: Basic class to create zipfiles
 */
class zipFile {
 public $files = array();
 public $settings = NULL;
 public $fileInfo = array (
   "name" => "",
   "numFiles" => 0,
   "fullFilePath" => ""
  );
 private $fileHash = "";
 private $zip = "";
 public function __construct($settings) {
  $this->zipFile($settings);
 }
 public function zipFile($settings) {
  $this->zip = new ZipArchive();
  $this->settings = new stdClass();
  foreach ($settings as $k => $v) {
   $this->settings->$k = $v;
  }
 }
 public function create() {
  $this->fileHash = md5(implode(",", $this->files));
  $this->fileInfo["name"] = $this->fileHash . ".zip";
  $this->fileInfo["numFiles"] = count($this->files);
  $this->fileInfo["fullFilePath"] = $this->settings->path . 
  "/" . $this->fileInfo["name"];
  if (file_exists($this->fileInfo["fullFilePath"])) {
   return array (
     false,
     "already created: " . $this->fileInfo["fullFilePath"]
     );
  }
  else {
   $this->zip->open($this->fileInfo["fullFilePath"], ZIPARCHIVE::CREATE);
   $this->addFiles();
   $this->zip->close();
   return array (
     true,
     "new file created: " . $this->fileInfo["fullFilePath"]
     );
  }
 }
 private function addFiles() {
  foreach ($this->files as $k) {
   $this->zip->addFile($k, basename($k));
  }
 }
}
$settings = array (
  "path" => dirname(__FILE__)
 );
$zipFile = new zipFile($settings);
$zipFile->files = array (
  "./images/navoff.jpg",
  "./images/navon.jpg"
 );
list($success, $error) = $zipFile->create();
if ($success === true) {
 //success
}
else {
 //error because: $error
}
?>

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

PHP 相关文章推荐
PHP数据集构建JSON格式及新数组的方法
Nov 07 PHP
深入密码加salt原理的分析
Jun 06 PHP
CI(CodeIgniter)框架介绍
Jun 09 PHP
php实现无限级分类
Dec 24 PHP
Zend Guard使用指南及问题处理
Jan 07 PHP
PHP获取数组最大值下标的方法
May 12 PHP
谈谈 PHP7新增功能
Dec 16 PHP
在PHP站点的页面上添加Facebook评论插件的实例教程
Jan 08 PHP
PHPWind9.0手动屏蔽验证码解决后台关闭验证码但是依然显示的问题
Aug 12 PHP
PHP实现限制IP访问及提交次数的方法详解
Jul 17 PHP
PHP htmlspecialchars_decode()函数用法讲解
Mar 01 PHP
PHP实现常用排序算法的方法
Feb 05 PHP
php生成图片缩略图的方法
Apr 07 #PHP
Nginx下配置codeigniter框架方法
Apr 07 #PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
Apr 07 #PHP
php修改文件上传限制方法汇总
Apr 07 #PHP
windows下安装php的memcache模块的方法
Apr 07 #PHP
CodeIgniter删除和设置Cookie的方法
Apr 07 #PHP
php获取网页里所有图片并存入数组的方法
Apr 06 #PHP
You might like
PHP 的几个配置文件函数
2006/12/21 PHP
基于PHP Web开发MVC框架的Smarty使用说明
2013/04/19 PHP
Symfony2学习笔记之插件格式分析
2016/03/17 PHP
非常好的js代码
2006/06/27 Javascript
javascript编程起步(第二课)
2007/02/27 Javascript
web基于浏览器的本地存储方法应用
2012/11/27 Javascript
javascript中window.event事件用法详解
2012/12/11 Javascript
常用的jquery模板插件——jQuery Boilerplate介绍
2014/09/23 Javascript
js实现图片漂浮效果的方法
2015/03/02 Javascript
drag-and-drop实现图片浏览器预览
2015/08/06 Javascript
JS获取鼠标选中的文字
2016/08/10 Javascript
BootStrap Fileinput初始化时的一些参数
2016/12/30 Javascript
微信小程序页面间通信的5种方式
2017/03/31 Javascript
jQuery结合jQuery.cookie.js插件实现换肤功能示例
2017/10/14 jQuery
vue 实现全选全不选的示例代码
2018/03/29 Javascript
Angular Renderer (渲染器)的具体使用
2018/05/03 Javascript
详解Nuxt.js中使用Element-UI填坑
2019/09/06 Javascript
python实现嵌套列表平铺的两种方法
2018/11/08 Python
python调用c++ ctype list传数组或者返回数组的方法
2019/02/13 Python
Python3利用print输出带颜色的彩色字体示例代码
2019/04/08 Python
Python3.5局部变量与全局变量作用域实例分析
2019/04/30 Python
jupyter note 实现将数据保存为word
2020/04/14 Python
python中编写函数并调用的知识点总结
2021/01/13 Python
selenium携带cookies模拟登陆CSDN的实现
2021/01/19 Python
你不知道的葡萄干处理法、橙蜜处理法、二氧化碳酵母法
2021/03/17 冲泡冲煮
Clarins娇韵诗英国官网:来自法国的天然护肤品牌
2017/04/18 全球购物
墨西哥巴士车票在线购买:ClickBus
2018/03/27 全球购物
AJAX的优缺点都有什么
2015/08/18 面试题
学习决心书范文
2014/03/11 职场文书
幼教求职信
2014/03/12 职场文书
乡镇信息公开实施方案
2014/03/23 职场文书
网络工程专业大学生求职信
2014/10/01 职场文书
思想作风建设心得体会
2014/10/22 职场文书
贪污受贿检讨书范文
2014/11/19 职场文书
MySQL高速缓存启动方法及参数详解(query_cache_size)
2021/07/01 MySQL
MongoDB日志切割的三种方式总结
2021/09/15 MongoDB