php生成zip文件類實例
更新時間:2015年04月07日 12:08:00 作者:不吃皮蛋
這篇文章主要介紹了php生成zip文件類,實例分析了php操作zip文件的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了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使用ZipArchive提示Fatal error: Class ZipArchive not found in的
這篇文章主要介紹了php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解決方法,是使用ZipArchive時經(jīng)常會遇到的問題,需要的朋友可以參考下2014-11-11
PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫和讀取數(shù)據(jù)操作實例詳解
這篇文章主要介紹了PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫和讀取數(shù)據(jù)操作,結合實例形式詳細分析了php5.5使用mysqli連接、讀取mysql數(shù)據(jù)庫,以及PDO預處理相關操作技巧,需要的朋友可以參考下2019-02-02
PHP使用CURL實現(xiàn)多線程抓取網(wǎng)頁
PHP 利用 Curl 可以完成各種傳送文件操作,比如模擬瀏覽器發(fā)送GET,POST請求等等,然而因為php語言本身不支持多線程,所以開發(fā)爬蟲程序效率并不高,不過可以用 Curl ,借助Curl 這個功能實現(xiàn)并發(fā)多線程的訪問多個url地址以實現(xiàn)并發(fā)多線程抓取網(wǎng)頁或者下載文件2015-04-04
php實現(xiàn)自定義中獎項數(shù)和概率的抽獎函數(shù)示例
這篇文章主要介紹了php實現(xiàn)自定義中獎項數(shù)和概率的抽獎函數(shù),涉及php字符串、數(shù)組的概率運算相關操作技巧,需要的朋友可以參考下2017-05-05

