php生成zip文件类实例

yipeiwu_com4年前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 在变量定义中不需要(或不支持)明确的类型定义;变量类型是根据使用该变量的上下文所决定的。也就是说,如果把一个字符串值赋给变量 var,var 就成了一个字符串。如果又把一个整型值...

PHP中文分词的简单实现代码分享

当然, 本文不是要对中文搜索引擎做研究, 而是分享如果用 PHP 做一个站内搜索引擎。 本文是这个系统中的一篇。 我使用的分词工具是中科院计算所的开源版本的 ICTCLAS。 另外还有开...

PHP标准类(stdclass)用法示例

本文实例讲述了PHP标准类(stdclass)用法。分享给大家供大家参考,具体如下: php是内置标准类的(stdclass) <?php $obj = new std...

php中检查文件或目录是否存在的代码小结

下面是一个简单的检查文件是否存在的实例代码: 复制代码 代码如下: <?php $filename = '/path/to/foo.txt'; if (file_exists($f...

php给图片加文字水印

注释非常的详细了,这里就不多废话了 <?php /*给图片加文字水印的方法*/ $dst_path = '/zb_users/upload/202003/qndictx...