PHP解壓ZIP文件到指定文件夾的方法
本文實(shí)例講述了PHP解壓ZIP文件到指定文件夾的方法。分享給大家供大家參考,具體如下:
/** * function: 解壓zip 格式的文件 * author:friker * date:2015-15-14 * reference:http://php.net/manual/zh/ref.zip.php * all rights reserved:wujiangwei123@126.com */ class Unzip{ public function __construct(){ //init code here... header("content-type:text/html;charset=utf8"); } /** * 解壓文件到指定目錄 * * @param string zip壓縮文件的路徑 * @param string 解壓文件的目的路徑 * @param boolean 是否以壓縮文件的名字創(chuàng)建目標(biāo)文件夾 * @param boolean 是否重寫已經(jīng)存在的文件 * * @return boolean 返回成功 或失敗 */ public function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true){ if ($zip = zip_open($src_file)){ if ($zip){ $splitter = ($create_zip_name_dir === true) ? "." : "/"; if($dest_dir === false){ $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/"; } // 如果不存在 創(chuàng)建目標(biāo)解壓目錄 $this->create_dirs($dest_dir); // 對(duì)每個(gè)文件進(jìn)行解壓 while ($zip_entry = zip_read($zip)){ // 文件不在根目錄 $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/"); if ($pos_last_slash !== false){ // 創(chuàng)建目錄 在末尾帶 / $this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1)); } // 打開包 if (zip_entry_open($zip,$zip_entry,"r")){ // 文件名保存在磁盤上 $file_name = $dest_dir.zip_entry_name($zip_entry); // 檢查文件是否需要重寫 if ($overwrite === true || $overwrite === false && !is_file($file_name)){ // 讀取壓縮文件的內(nèi)容 $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); @file_put_contents($file_name, $fstream); // 設(shè)置權(quán)限 chmod($file_name, 0777); echo "save: ".$file_name."<br />"; } // 關(guān)閉入口 zip_entry_close($zip_entry); } } // 關(guān)閉壓縮包 zip_close($zip); } }else{ return false; } return true; } /** * 創(chuàng)建目錄 */ public function create_dirs($path){ if (!is_dir($path)){ $directory_path = ""; $directories = explode("/",$path); array_pop($directories); foreach($directories as $directory){ $directory_path .= $directory."/"; if (!is_dir($directory_path)){ mkdir($directory_path); chmod($directory_path, 0777); } } } } } /* using: $z = new Unzip(); $z->unzip("./bootstrap-3.3.4.zip",'./unzipres/', true, false); */
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP操作zip文件及壓縮技巧總結(jié)》、《php文件操作總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php實(shí)現(xiàn)模擬post請(qǐng)求用法實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)模擬post請(qǐng)求用法,分析了php模擬post請(qǐng)求的三種常見用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Centos 6.5下PHP 5.3安裝ffmpeg擴(kuò)展的步驟詳解
大家都知道ffmpeg是一款視頻流的軟件了,我們?cè)趌inux系統(tǒng)中可以安裝ffmpeg了,這篇文章主要介紹了在Centos 6.5下PHP 5.3安裝ffmpeg擴(kuò)展的步驟,需要的朋友可以參考下。2017-03-03PHP實(shí)現(xiàn)求解最長(zhǎng)公共子串問題的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)求解最長(zhǎng)公共子串問題的方法,簡(jiǎn)單描述了求解最長(zhǎng)公共子串問題算法原理,并結(jié)合實(shí)例形式分析了PHP實(shí)現(xiàn)求解最長(zhǎng)公共子串的具體操作技巧,需要的朋友可以參考下2017-11-11PHP常用字符串函數(shù)用法實(shí)例總結(jié)
這篇文章主要介紹了PHP常用字符串函數(shù)用法,結(jié)合實(shí)例形式總結(jié)分析了PHP常用字符串函數(shù)基本功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-06-06php intval的測(cè)試代碼發(fā)現(xiàn)問題
測(cè)試php intval函數(shù)的代碼:2008-07-07PHP實(shí)現(xiàn)在數(shù)據(jù)庫(kù)百萬條數(shù)據(jù)中隨機(jī)獲取20條記錄的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)在數(shù)據(jù)庫(kù)百萬條數(shù)據(jù)中隨機(jī)獲取20條記錄的方法,涉及php簡(jiǎn)單數(shù)據(jù)庫(kù)查詢與隨機(jī)數(shù)操作相關(guān)技巧,需要的朋友可以參考下2017-04-04php discuz 主題表和回帖表的設(shè)計(jì)
看看網(wǎng)上優(yōu)秀程序的源碼,分析優(yōu)缺點(diǎn),快速提高我們的水平。2009-03-03