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基于CURL发送JSON格式字符串的方法示例

本文实例讲述了PHP基于CURL发送JSON格式字符串的方法。分享给大家供大家参考,具体如下: /* * post 发送JSON 格式数据 * @param $url string...

Windows下wamp php单元测试工具PHPUnit安装及生成日志文件配置方法

本文实例讲述了Windows下wamp php单元测试工具PHPUnit安装及生成日志文件配置方法。分享给大家供大家参考,具体如下: phpunit下载网站 http://www.php...

试用php中oci8扩展

给大家分享个php操作Oracle的操作类 Oracle_db.class.php <?php class Oracle_db{ public $link; p...

Cannot modify header information错误解决方法

<?php ob_start(); setcookie("username","宋岩宾",time()+3600); echo "the username is:".$HTTP_C...

AJAX的使用方法详解

AJAX作为异步传输,局部刷新非常方便,用处很广! 首先,对于AJAX的使用有4步: 1.创建AJAX对象 var xmlHttp = new XMLHttpRequest(); 2.建...