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 删除一个数组中的某个值.兼容多维数组!

复制代码 代码如下: function array_remove_key($array, $keys) { $num = count($keys); $num_last = $num -...

php_pdo 预处理语句详解

这篇文章主要介绍的是关于php_pdo 预处理语句,下面话不多说,我们来看看详细的内容。 一、预处理语句可以带来两大好处: 1、查询仅需解析(或预处理)一次,但可以用相同或不同的参数执行...

PHP实现的数独求解问题示例

本文实例讲述了PHP实现的数独求解问题。分享给大家供大家参考,具体如下: 一、数独问题描述: 对于给出的数字二维数组,要求每行每列的数字不能重复。 二、实现代码: <?...

php ss7.5的数据调用 (笔记)

php ss7.5的数据调用 (笔记)

这几天搞 ss7.5 dz7.2 uc1.5 uchome2.0和自己主站的整合 头都大了 呵呵  好歹是弄的差不多 了 呵呵 记录一下 ss7.5的数据调用  ...

PHP实现向关联数组指定的Key之前插入元素的方法

本文实例讲述了PHP实现向关联数组指定的Key之前插入元素的方法。分享给大家供大家参考,具体如下: PHP 关联数组可以通过三种方式插入新元素: 1. $array[$insert_ke...