|
<?php
error_reporting(2047);
/*
* Class IO (SNakeVil 完成 03.25.04) (v1.0.0.0)
*
* [說(shuō)明]
* 本類用于對(duì)文件系統(tǒng)的處理。
*
* [功能]
* **** list_dir($dir_path);
* 讀取指定目錄內(nèi)容,返回內(nèi)容數(shù)組。
* $dir_path 字符串,指定目錄路徑
* 若有錯(cuò)誤返回 FALSE,否則返回
* array(
* "count"=>array("files","dirs","size"),
* "list"=>array(
* array("name","locate","type","size","last_access","last_change","last_modify"),
* ......
* )
* )
* ********
* ********
* **** seek_file($pattern, $dir_path, $seek_type, $sub_dir, $interal, $limit);
* 根據(jù)正則表達(dá)式條件,在相應(yīng)目錄及給定層次的子目錄中搜索匹配的文件、目錄。
* $pattern 符合 PERL 兼容標(biāo)準(zhǔn)的正則表達(dá)式,無(wú)須添加 //,系統(tǒng)自行添加
* $seek_type 有 -1 0 1 三種可能值,0 僅文件夾,1 僅文件,-1 兩者都包括
* $sub_dir 數(shù)字值,搜索的子目錄深度,指定目錄不算,建議不要超過(guò) 5
* $interal 布爾值,為真則返回搜索結(jié)果的詳細(xì)信息,否則只返回文件名、類型及所在目錄
* $limit 數(shù)字值,搜索結(jié)果限制,避免過(guò)度浪費(fèi)系統(tǒng)資源
* 若有錯(cuò)誤返回 FALSE,否則返回
* array(
* array(
* "name","locate","type"
* [,"size","last_access","last_change","last_modify"]
* ),
* ......
* )
* ********
* ********
* **** delete($path);
* 刪除指定對(duì)象,文件或文件夾――包括內(nèi)含子目錄和文件的非空文件夾。
* $path 字符串,指定要?jiǎng)h除的內(nèi)容路徑,文件或目錄均可
* 如有錯(cuò)誤在錯(cuò)誤處中斷,返回 FALSE,否則返回 TRUE
* ********
* ********
* **** make_dir($path);
* 建立任意文件夾,相對(duì)或絕對(duì)路徑皆可,深層建立亦可。
* $path 字符串,要建立的最終目錄路徑
* 如有錯(cuò)誤返回 FALSE,否則返回 TRUE
* ********
* ********
* **** verify_file($src, $dst, $interal);
* 使用 MD5 算法比較兩個(gè)文件是否相同。
* $src 字符串,源文件路徑
* $dst 字符串,目標(biāo)文件路徑
* $interal 布爾值,對(duì)于大于 1M 文件,可以設(shè)置為 FALSE 以省去 MD5 檢驗(yàn)步驟,減輕服務(wù)器負(fù)擔(dān)
* 若有錯(cuò)誤返回 FALSE,否則返回 TRUE
* ********
* ********
* **** copy($src_path, $dst_path);
* 對(duì)任意文件夾、文件進(jìn)行復(fù)制,相對(duì)或絕對(duì)路徑皆可,文件復(fù)制完成后會(huì)進(jìn)行效驗(yàn),檢查是否出錯(cuò)數(shù)據(jù)錯(cuò)誤。
* $src_path 字符串,指定要復(fù)制的源內(nèi)容路徑,文件或目錄均可
* $dst_path 字符串,指定要復(fù)制的目標(biāo)內(nèi)容路徑,文件或目錄均可,性質(zhì)由 $src_path 決定,可為 $src_path 下層目錄
* 若有錯(cuò)誤返回 FALSE,否則返回 TRUE
* ********
* ********
* **** move($src_path, $dst_path);
* 對(duì)任意文件夾、文件進(jìn)行移動(dòng),相對(duì)或絕對(duì)路徑皆可,文件移動(dòng)完成后會(huì)進(jìn)行效驗(yàn),檢查是否出錯(cuò)數(shù)據(jù)錯(cuò)誤。
* $src_path 字符串,指定要移動(dòng)的源內(nèi)容路徑,文件或目錄均可
* $dst_path 字符串,指定要移動(dòng)的目標(biāo)內(nèi)容路徑,文件或目錄均可,性質(zhì)由 $src_path 決定,可為 $src_path 下層目錄
* 若有錯(cuò)誤返回 FALSE,否則返回 TRUE
*
* [版權(quán)]
* 風(fēng)雨明清(SNakeVil@51js, SNakeVil@BU)獨(dú)立設(shè)計(jì)完成,保留一切權(quán)力。
* 隨意使用,但請(qǐng)勿必保留下面的文本,謝謝!
*
* ===========Z=================
* Class.IO.v1.0.0.0.build040325
* for.php.v4.20+
* by SNakeVil
* (snakevil@51js, snakevil@BU)
* --------+------
* QQ:118824
* MSN:snakevil_@hotmail.com
* HP:<a >http://www.snakevil.com/</a>
* ===========Z=================
*
*/
class IO {
var $error_id;
var $result;
var $error_related;
var $last_exist_dir;
function IO() {
$this->result = array();
$this->error_id = 0x0000;
$this->error_related = "";
$this->last_exist_dir = "";
return $this;
}
function error_occur($error_id=0xffff,$error_related="") { // ----0xffff---- 發(fā)生錯(cuò)誤,但錯(cuò)誤原因未知
if (is_int($error_id)) $this->error_id = $error_id; // 獲取錯(cuò)誤號(hào)
$this->error_related = $error_related;
return false; // 錯(cuò)誤發(fā)生時(shí)返回 FALSE 方便進(jìn)一步處理
}
function list_dir($dir_path=".") {
if (!is_dir($dir_path)) return $this->error_occur(0x0001, $dir_path); // ----0x0001---- 指定目錄不存在
if (!$dir_handle=@opendir($dir_path)) return $this->error_occur(0x0002, $dir_path); // ----0x0002---- 指定目錄無(wú)權(quán)讀取
$result = array(
"count" => array("files" => 0, "dirs" => 0, "size" => 0),
"list" => array()
);
while (false!==($file_handle=readdir($dir_handle))) { // 使用 !== 防止處理名稱為 0 或 FALSE 的文件、目錄
if ($file_handle=="."||$file_handle=="..") continue; // 忽略系統(tǒng)特定的兩個(gè)文件夾
$temp = str_replace("/", "/", realpath($dir_path));
$temp = substr($temp, -1)=="/" ? $temp : $temp."/";
$temp = array($temp, $file_handle);
$file_handle = $temp[0].$temp[1]; // 獲取絕對(duì)地址
$temp = array(
"name" => $temp[1],
"locate" => $temp[0],
"type" => @filetype($file_handle),
"size" => filesize($file_handle),
"last_access" => fileatime($file_handle),
"last_modify" => filemtime($file_handle),
"last_change" => filectime($file_handle)
);
switch ($temp["type"]) {
case "file":
$temp["type"] = 1;
$result["count"]["files"]++;
$result["count"]["size"] += $temp["size"];
break;
case "dir":
$temp["type"] = 0;
$result["count"]["dirs"]++;
break;
default: // !!!! 鑒于 Win32 平臺(tái),對(duì)既非文件也非目錄的內(nèi)容忽略
$temp["type"] = -1;
}
$result["list"][] = $temp;
}
closedir($dir_handle);
unset($dir_handle, $file_handle, $temp);
clearstatcache(); // 清除文件系統(tǒng)緩存
return $this->result = $result;
}
function seek_file($pattern=".*",$dir_path=".",$seek_type=1,$sub_dir=0,$interal=false,$limit=100) {
/* 規(guī)范一切可能的參數(shù)值 */
$pattern = "/".$pattern."/";
$seek_type = intval($seek_type);
$seek_type = $seek_type>0 ? 1 : ($seek_type<0 ? -1 : 0);
$sub_dir = abs(intval($sub_dir));
$interal = (bool)$interal;
$limit = abs(intval($limit));
if ($limit==0) $limit = 100;
$sub_dir_list = array(array($dir_path)); // 將查詢目錄作為子目錄層次的第一層來(lái)對(duì)待
$result = array();
/* i 當(dāng)前處理的子目錄層次,0 為指定目錄層,即僅處理一個(gè)目錄 */
for ($i=0;$i<=$sub_dir;$i++) {
if (!isset($sub_dir_list[$i])) return $this->result = $result; // 如果某一層子目錄沒(méi)有設(shè)置,說(shuō)明實(shí)際目錄系統(tǒng)中再無(wú)目錄,返回
/* k 每一子目錄層次中子目錄統(tǒng)計(jì),j 當(dāng)前處理序號(hào) */
for ($j=0,$k=count($sub_dir_list[$i]);$j<$k;$j++) { // 根據(jù)每一層子目錄數(shù)量處理
$l = $this->list_dir($sub_dir_list[$i][$j]);
if (!$l) return $this->result = $result; // 出現(xiàn)錯(cuò)誤,則立即停止返回現(xiàn)有結(jié)果
$l = $l["list"];
/* n 每一子目錄中文件、目錄、其他項(xiàng)目統(tǒng)計(jì),m 為當(dāng)前處理序號(hào) */
for ($m=0,$n=count($l);$m<$n;$m++) {
if (count($result)>=$limit) return $this->result = $result; // 如果要求數(shù)目已達(dá)到,返回
if ($l[$m]["type"]==0) $sub_dir_list[$i+1][] = $l[$m]["locate"].$l[$m]["name"]; // 搜集下一層子目錄信息
$o = $l[$m]["type"];
if ($o!=$seek_type&&($seek_type==1||$seek_type==0)) continue; // 忽略不符合要求的項(xiàng)目
elseif ($o==-1&&$seek_type==-1) continue;
if (!preg_match($pattern, $l[$m]["name"])) continue; // 忽略不符合正則表達(dá)式的項(xiàng)目
$result[] = $interal ? $l[$m] : array("name" => $l[$m]["name"], "locate" => $l[$m]["locate"], "type" => $l[$m]["type"]);
}
}
}
unset($i, $j, $k, $l, $m, $n, $o, $sub_dir_list);
return $this->result = $result;
}
function delete($path="") {
if (!file_exists($path)) return $this->error_occur(0x0003, $path); // ----0x0003---- 指定對(duì)象不存在
if (is_dir($path)) {
$path = str_replace("", "/", realpath($path));
$path = substr($path, -1)=="/" ? $path : $path."/";
$sub_list = array(array($path));
for ($i=0;$i<count($sub_list);$i++) { // 使用 COUNT($SUB_LIST) 動(dòng)態(tài)判斷長(zhǎng)度,從而有可能無(wú)定長(zhǎng)循環(huán)
if (!isset($sub_list[$i])) break; // 探索到最盡頭,獲得該目錄下所有子目錄列表,方便文件刪除后刪除目錄
for ($j=0,$k=count($sub_list[$i]);$j<$k;$j++) {
$l = $this->list_dir($sub_list[$i][$j]);
if (!$l) return $this->error_occur("", $sub_list[$i][$j]);
$l = $l["list"];
for ($m=0,$n=count($l);$m<$n;$m++) {
$o = $l[$m]["locate"].$l[$m]["name"];
if ($l[$m]["type"]==0) $sub_list[$i+1][] = $o;
elseif (!@unlink($o)) return $this->error_occur(0x0004, $o); // 刪除目錄下的每一個(gè)文件
}
}
}
for($i=count($sub_list)-1;$i>=0;$i--) // 逆回刪除目錄
for ($j=0,$k=count($sub_list[$i]);$j<$k;$j++) // 刪除每一個(gè)子目錄直到指定目錄
if (!@rmdir($sub_list[$i][$j])) return $this->error_occur(0x0005, $sub_list[$i][$j]); // ----0x0005---- 目錄無(wú)權(quán)刪除
unset($i, $j, $k, $l, $m, $n, $o, $sub_list);
return true;
} elseif (@unlink($path)) return true;
else return $this->error_occur(0x0004, $path); // ----0x0004---- 文件無(wú)權(quán)刪除
}
function generate_realpath($path="") {
if ($path==""||!is_string($path)) return $this->error_occur(0x0007, $path); // ----0x0007---- 路徑參數(shù)錯(cuò)誤
$path = preg_replace("/(?<!^w)[:*?"<>|]/", "", str_replace("/", "/", $path)); // 規(guī)范路徑中多可能性的符號(hào)
if (substr($path,1,1)==":") return $path; // !!!! Win32 平臺(tái)的絕對(duì)路徑
elseif (substr($path,0,1)=="/") return substr(realpath("."), 0, 2).$path; // !!!! Win32 平臺(tái)下的絕對(duì)路徑轉(zhuǎn)換
else {
if (substr($path,-1)=="/") $path = substr($path,0,-1); // 清除結(jié)尾可能的 / 符號(hào)
$path = preg_replace("http://{2,}/", "/", $path); // 將 /// 諸如類似的相連符號(hào)簡(jiǎn)化為一個(gè)
$path = explode("/", $path); // 分割路徑
$cur_path = explode("/", str_replace("/", "/", realpath(".")));
for ($i=0,$j=count($path);$i<$j;$i++) {
if ($path[$i]=="..") array_pop($cur_path);
elseif ($path[$i]=="."||$path[$i]==str_repeat(".", strlen($path[$i]))) continue; // 忽略無(wú)用的相對(duì)路徑地址 . 和 .... 等
else array_push($cur_path, $path[$i]);
}
$path = implode("/", $cur_path);
unset($cur_path);
return $path;
}
}
function make_dir($path="") {
if (!$path=$this->generate_realpath($path)) return false;
$path = explode("/", $path);
$i = array($path[0]);
for ($i=0,$j=count($path),$k=array(),$l="";$i<$j;$i++) {
array_push($k, $path[$i]);
$l = implode("/", $k);
if (!file_exists($l)) {
if ($this->last_exist_dir=="") $this->last_exist_dir = $l;
if (!@mkdir($l)) return $this->error_occur(0x0008, $l); // ----0x0008---- 無(wú)法創(chuàng)建目錄
}
}
return true;
}
function verify_file($src="",$dst="",$interal=true) {
if (!file_exists($src)||!is_file($src)) return $this->error_occur(0x000A, $src); // ----0x000A---- 指定對(duì)象非文件
if (!file_exists($dst)||!is_file($dst)) return $this->error_occur(0x000A, $dst);
$i = filesize($src);
if ($i!=filesize($dst)) {
unset($i);
return false;
}
if ($i>1024*1024*1024&&!$interal) { // 對(duì)于大于 1MB 的文件,如果不要求精確檢查,跳過(guò)
unset($i);
return true;
}
unset($i);
if (md5_file($src)!=md5_file($dst)) return false;
return true;
}
function copy($src_path="",$dst_path="") {
if (!file_exists($src_path)) return $this->error_occur(0x0003, $src_path);
if (!$dst_path=$this->generate_realpath($dst_path)) return false;
if (is_dir($src_path)) {
$this->last_exist_dir = ""; // 記錄現(xiàn)行實(shí)際存在的目錄
if (!$this->make_dir($dst_path)) return false; // 建立目錄失敗
$src_path = str_replace("", "/", realpath($src_path));
$src_path = substr($src_path, -1)=="/" ? $src_path : $src_path."/";
$sub_list = array(array($src_path));
for ($i=0;$i<count($sub_list);$i++) {
if (!isset($sub_list[$i])) break;
for ($j=0,$k=count($sub_list[$i]);$j<$k;$j++) {
$l = $this->list_dir($sub_list[$i][$j]);
if (!$l) return $this->error_occur(0x0003, $sub_list[$i][$j]);
$l = $l["list"];
for ($m=0,$n=count($l);$m<$n;$m++) {
$o = $l[$m]["locate"].$l[$m]["name"];
if ($o==$this->last_exist_dir) continue; // 如果為上級(jí)目錄向下級(jí)目錄復(fù)制,防止死循環(huán)
$p = str_replace(substr($src_path, 0, -1), $dst_path, $o);
if ($l[$m]["type"]==0) {
$sub_list[$i+1][] = $o;
if (!$this->make_dir($p)) return false; // 對(duì)每一個(gè)子目錄都予以建立
} else { // 對(duì)每一個(gè)文件進(jìn)行復(fù)制
if ($this->verify_file($o, $p)) continue; // 如果目標(biāo)與源完全相同,不再?gòu)?fù)制
if (!copy($o,$p)||!$this->verify_file($o,$p)) return $this->error_occur(0x0009, $o); // ----0x0009---- 文件移動(dòng)失敗
}
}
}
}
unset($i, $j, $k, $l, $m, $n, $o, $p, $sub_list);
return true;
} else {
if (!is_readable($src_path)) return $this->error_occur(0x0006, $src_path); // ----0x0006---- 源文件無(wú)權(quán)讀取
if ($this->verify_file($src_path,$dst_path)) return true;
$i = strrpos($dst_path, "/");
$dst_path = array(substr($dst_path, 0, $i), substr($dst_path, $i+1));
unset($i);
if (!$this->make_dir($dst_path[0])) return false;
$dst_path = implode("/", $dst_path);
if (!copy($src_path,$dst_path)||!$this->verify_file($src_path,$dst_path)) return $this->error_occur(0x0009, $src_path);
return true;
}
}
function move($src_path="",$dst_path="") {
if (!file_exists($src_path)) return $this->error_occur(0x0003, $src_path);
if (!$dst_path=$this->generate_realpath($dst_path)) return false;
if (is_dir($src_path)) {
$this->last_exist_dir = "";
if (!$this->make_dir($dst_path)) return false;
$src_path = str_replace("", "/", realpath($src_path));
$src_path = substr($src_path, -1)=="/" ? $src_path : $src_path."/";
$sub_list = array(array($src_path));
for ($i=0;$i<count($sub_list);$i++) {
if (!isset($sub_list[$i])) break;
for ($j=0,$k=count($sub_list[$i]);$j<$k;$j++) {
$l = $this->list_dir($sub_list[$i][$j]);
if (!$l) return $this->error_occur(0x0003, $sub_list[$i][$j]);
$l = $l["list"];
for ($m=0,$n=count($l);$m<$n;$m++) {
$o = $l[$m]["locate"].$l[$m]["name"];
if ($o==$this->last_exist_dir) continue;
$p = str_replace(substr($src_path, 0, -1), $dst_path, $o);
if ($l[$m]["type"]==0) {
$sub_list[$i+1][] = $o;
if (!$this->make_dir($p)) return false;
} else {
if ($this->verify_file($o, $p)) continue;
if (!copy($o,$p)||!$this->verify_file($o,$p)) return $this->error_occur(0x0009, $o);
if (!@unlink($o)) return $this->error_occur(0x0004, $o);
}
}
}
}
for($i=count($sub_list)-1;$i>=0;$i--)
for ($j=0,$k=count($sub_list[$i]);$j<$k;$j++)
if (strpos($this->last_exist_dir,$sub_list[$i][$j])!==false) continue; // 對(duì)移動(dòng)目標(biāo)目錄的上層目錄,不予考慮刪除
elseif (!@rmdir($sub_list[$i][$j])) return $this->error_occur(0x0005, $sub_list[$i][$j]);
unset($i, $j, $k, $l, $m, $n, $o, $p, $sub_list);
return true;
} else {
if (!is_readable($src_path)) return $this->error_occur(0x0006, $src_path);
if ($this->verify_file($src_path,$dst_path)) return true;
$i = strrpos($dst_path, "/");
$dst_path = array(substr($dst_path, 0, $i), substr($dst_path, $i+1));
unset($i);
if (!$this->make_dir($dst_path[0])) return false;
$dst_path = implode("/", $dst_path);
if (!copy($src_path,$dst_path)||!$this->verify_file($src_path,$dst_path)) return $this->error_occur(0x0009, $src_path);
if (@unlink($src_path)) return true;
else return $this->error_occur(0x0004, $src_path);
}
}
}
?>
php技術(shù):文件系統(tǒng)基本操作類,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。