php生成zip文件类实例

yipeiwu_com5年前PHP代码库

本文实例讲述了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求今天、昨天、明天时间戳的简单实现方法。分享给大家供大家参考,具体如下: echo strtotime('now'),'<br>';//现在 echo...

四个PHP非常实用的功能

最近写的几个PHP实用功能整理了一下,弄成一个文档,写上说明,方便以后使用!一共有4个PHP实用功能,现在跟大家分享,喜欢的朋友可以把它收藏起来,说不定以后用得上。 1. PHP函数的任...

PHP使用DOM和simplexml读取xml文档的方法示例

本文实例讲述了PHP使用DOM和simplexml读取xml文档的方法。分享给大家供大家参考,具体如下: 实例  用DOM获取下列xml文档中所有金庸小说的书名,该xml文档所...

thinkPHP的Html模板标签使用方法

注意:在使用如<html:select />等标签之前,必须要引入TP的标签库:<tagLib name="html" /> 如果我们现在需要一个select下拉...

PHP获取对象属性的三种方法实例分析

本文实例讲述了PHP获取对象属性的三种方法。分享给大家供大家参考,具体如下: 今天查看yii源码,发现yii\base\Model中的attribute()方法是通过反射获取对象的pub...