原创:IO 类 (主要为常用文件系统操作)
闲暇时信手写的一个类,后来感觉有用就整理了一下,发上来给大家 pp
功能谈不上强大,就把自己想得到的功能实现了
欢迎大家提意见~
1----------------------------------------
/*
*
* ===========Z=================
* QQ:118824
* MSN:snakevil_@hotmail.com
* HP:http://www.snakevil.com/
* ===========Z=================
*
*/
/**
* @]Class Name[= IO
* @]Class URI[= System.IO
* @]Purpose[=
* 本类用于对文件系统的处理
* @]Author[= SNakeVil<51JS,BU>(snakevil@qq.com)
* @]Version[= 1.0.1
* @]Create[= 17:13 2004-3-25
* @]Modifications[=
* 7:31 2004-3-27
* + 将错误处理抽象为基类
* + 增加方法 comment_away()
* @]See[=
*/
class IO extends SnkClass {
var $result;
var $last_exist_dir;
function IO() {
parent::SnkClass();
$this->result = array();
$this->error_id = 0x0000;
$this->error_related = "";
$this->last_exist_dir = "";
return $this;
}
/**
* @]Method Name[= list_dir()
* @]Purpose[=
* 读取指定目录内容,返回内容数组
* @]Parameter[=
* string $dir_path 指定目录路径
* @]Return[= mixed 错误返回 FALSE,否则
* array(
* "count"=>array("files","dirs","size"),
* "list"=>array(
* array("name","locate","type","size","last_access","last_change","last_modify"),
* ......
* )
* )
* @]Author[= SNakeVil<51JS,BU>(snakevil@qq.com)
* @]See[=
*/
function list_dir($dir_path=".") {
if (!is_dir($dir_path)) return $this->error_occur(0x0001, $dir_path);
if (!$dir_handle=@opendir($dir_path)) return $this->error_occur(0x0002, $dir_path);
$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; // 忽略系统特定的两个文件夹
$temp = str_replace("\\", "/", realpath($dir_path));
$temp = substr($temp, -1)=="/" ? $temp : $temp."/";
$temp = array($temp, $file_handle);
$file_handle = $temp[0].$temp[1]; // 获取绝对地址
$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 平台,对既非文件也非目录的内容忽略
$temp["type"] = -1;
}
$result["list"][] = $temp;
}
closedir($dir_handle);
unset($dir_handle, $file_handle, $temp);
clearstatcache(); // 清除文件系统缓存
return $this->result = $result;
}
/**
* @]Method Name[= seek_file()
* @]Purpose[=
* 根据正则表达式条件,在相应目录及给定层次的子目录中搜索匹配的文件、目录
* @]Parameter[=
* regular $pattern 兼容 PERL 标准,无 //,指明搜索匹配要求
* enum $seesk_type 有 -1 0 1 三种可能值,0 仅文件夹,1 仅文件,-1 两者都包括
* int $sub_dir 搜索的子目录深度,指定目录不算,建议不要超过 5
* boolean $interal 为真则返回搜索结果的详细信息,否则只返回文件名、类型及所在目录
* limit $limit 搜索结果限制,避免过度浪费系统资源
* @]Return[= mixed 错误返回 FALSE,否则
* array(
* array(
* "name","locate","type"
* [,"size","last_access","last_change","last_modify"]
* ),
* ......
* )
* @]Author[= SNakeVil<51JS,BU>(snakevil@qq.com)
* @]See[=
*/
function seek_file($pattern=".*",$dir_path=".",$seek_type=1,$sub_dir=0,$interal=false,$limit=100) {
/* 规范一切可能的参数值 */
$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)); // 将查询目录作为子目录层次的第一层来对待
$result = array();
/* i 当前处理的子目录层次,0 为指定目录层,即仅处理一个目录 */
for ($i=0;$i<=$sub_dir;$i++) {
if (!isset($sub_dir_list[$i])) return $this->result = $result; // 如果某一层子目录没有设置,说明实际目录系统中再无目录,返回
/* k 每一子目录层次中子目录统计,j 当前处理序号 */
for ($j=0,$k=count($sub_dir_list[$i]);$j<$k;$j++) { // 根据每一层子目录数量处理
$l = $this->list_dir($sub_dir_list[$i][$j]);
if (!$l) return $this->result = $result; // 出现错误,则立即停止返回现有结果
$l = $l["list"];
/* n 每一子目录中文件、目录、其他项目统计,m 为当前处理序号 */
for ($m=0,$n=count($l);$m<$n;$m++) {
if (count($result)>=$limit) return $this->result = $result; // 如果要求数目已达到,返回
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; // 忽略不符合要求的项目
elseif ($o==-1&&$seek_type==-1) continue;
if (!preg_match($pattern, $l[$m]["name"])) continue; // 忽略不符合正则表达式的项目
$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;
}