这篇文章主要为大家详细介绍了php 生成zip文件的类的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
<?php
/**
* 生成zip文件类
*
* @param
* @arrange (512.笔记) www.q1010.com
**/
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
}
/*** 来自:四海网(www.q1010.com) ***/
?>
本文来自:http://www.q1010.com/173/548-0.html
注:关于php 生成zip文件的类的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:zip
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。