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连接oracle数据库的方法(测试成功)

本文简单分析了php连接oracle数据库的方法。分享给大家供大家参考,具体如下: PHP提供了两套函数与Oracle连接,分别是ORA_和OCI函数。其中ORA_函数略显陈旧。OCI函...

ThinkPHP安装和设置

前提:此教程适用于ThinkPHP 3.2+ 今天起的以后几天,将放出ThinkPHP的系列教程,一共七篇,需要的同学可以自行点赞收藏。 1.安装 安装ThinkPHP的方法很多,你可以...

php 函数中使用static的说明

复制代码 代码如下: function sendHeader($num, $rtarr = null) { static $sapi = null; if ($sapi === null...

php查询mssql出现乱码的解决方法

本文实例讲述了php查询mssql出现乱码的解决方法。分享给大家供大家参考。具体分析如下: 在php连接mssql时查询出来的全部是乱码,这种问题我根据经验知道是编码问题,下面来给各位总...

PHP iconv()函数字符编码转换的问题讲解

在php中iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库;但有时候iconv对于部分数据转码会无缘无故的少一些。比如在转换字符"—"到gb2312时会出错...