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