相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡(jiǎn)單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說(shuō)明
- thinkPHP5框架實(shí)現(xiàn)分頁(yè)查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫(xiě)文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
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 是否重寫(xiě)已經(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)); } // 打開(kāi)包 if (zip_entry_open($zip,$zip_entry,"r")){ // 文件名保存在磁盤(pán)上 $file_name = $dest_dir.zip_entry_name($zip_entry); // 檢查文件是否需要重寫(xiě) 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); */