Home >Backend Development >PHP Tutorial >Basic file system operation classes_PHP tutorial

Basic file system operation classes_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:00:41919browse

Copy code The code is as follows:

error_reporting(2047);

/*
* Class IO (SNakeVil completed 03.25.04) (v1.0.0.0)
*
* [Description]
* This class is used to process the file system.
*
* [Function]
* **** list_dir($dir_path);
* Read the contents of the specified directory and return the content array.
* $dir_path string, specify the directory path
* If there is an error, return FALSE, otherwise return
* 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, $internal, $limit);
* Search for matching files in the corresponding directory and subdirectories of a given level based on regular expression conditions ,Table of contents.
* $pattern is a regular expression that complies with PERL compatibility standards, no need to add //, the system adds it by itself
* $seek_type has three possible values ​​-1 0 1, 0 only folders, 1 only files, -1 Both include
* $sub_dir numeric value, the depth of the subdirectory searched, the specified directory does not count, it is recommended not to exceed 5
* $internal Boolean value, if true, return the detailed information of the search results, otherwise only return File name, type and directory
* $limit numeric value, search result limit to avoid excessive waste of system resources
* Returns FALSE if there is an error, otherwise returns
* array(
* array(
* "name","locate","type"
* [,"size","last_access","last_change","last_modify"]
* ),
* .... ..
* )
* ********
* ********
* **** delete($path);
* Delete Specifies an object, file, or folder—including non-empty folders containing subdirectories and files.
* $path string, specify the content path to be deleted, either a file or directory
* If there is an error and interrupt at the error, return FALSE, otherwise return TRUE
* ******* *
* ********
* **** make_dir($path);
* Create any folder, either a relative or absolute path, or deep creation.
* $path string, the final directory path to be created
* Returns FALSE if there is an error, otherwise returns TRUE
* ********
* ****** **
* **** verify_file($src, $dst, $internal);
* Use MD5 algorithm to compare whether two files are the same.
* $src string, source file path
* $dst string, target file path
* $internal Boolean value, for files larger than 1M, it can be set to FALSE to save the MD5 verification step and reduce Server burden
* Returns FALSE if there is an error, otherwise returns TRUE
* ********
* ********
* **** copy($ src_path, $dst_path);
* Copy any folder or file, either a relative or absolute path. After the file copy is completed, it will be verified to check whether there are any errors or data errors.
* $src_path String, specifies the source content path to be copied, either a file or directory
* $dst_path String, specifies the target content path to be copied, either a file or directory, the nature is determined by $src_path , can be the lower directory of $src_path
* If there is an error, return FALSE, otherwise return TRUE
* ********
* ********
* ** ** move($src_path, $dst_path);
* Move any folder or file, either relative or absolute path. After the file move is completed, it will be verified to check whether there are any errors or data errors.
* $src_path String, specifies the source content path to be moved, either a file or directory
* $dst_path String, specifies the target content path to be moved, either a file or directory, the nature is determined by $src_path , can be $src_path lower directory
* Returns FALSE if there is an error, otherwise returns TRUE
*
* [Copyright]
* Independently designed by Fengyu Mingqing (SNakeVil@51js, SNakeVil@BU) , all rights reserved.
* Feel free to use it, but please do not keep the text below, thank you! 

* ===========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:http://www.snakevil.com/ 
* ===========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---- 发生错误,但错误原因未知 
if (is_int($error_id)) $this->error_id = $error_id; // 获取错误号 
$this->error_related = $error_related; 
return false; // 错误发生时返回 FALSE 方便进一步处理 


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---- 指定目录无权读取 
$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; 
}

function seek_file($pattern=".*",$dir_path=".",$seek_type=1,$sub_dir=0,$interal=false,$limit=100) {
/* Standardize all possible parameter values ​​*/
$pattern = "/".$pattern."/";
$seek_type = intval($seek_type);
$seek_type = $seek_type>0 ? 1 : ($seek_type<0 ? -1 : 0);
$sub_dir = abs(intval($sub_dir));
$internal = (bool)$interal;
$limit = abs(intval($ limit));
if ($limit==0) $limit = 100;
$sub_dir_list = array(array($dir_path)); // Treat the query directory as the first level of the subdirectory hierarchy
$result = array();
/* i The subdirectory level currently being processed, 0 is the specified directory level, that is, only one directory is processed */
for ($i=0;$i<= $sub_dir;$i++) {
if (!isset($sub_dir_list[$i])) return $this->result = $result; // If a certain level of subdirectory is not set, it means that the actual directory system If there is no more directory, return
/* k subdirectory statistics in each subdirectory level, j current processing sequence number */
for ($j=0,$k=count($sub_dir_list[$i]); $j<$k;$j++) { // Processed according to the number of subdirectories at each level
$l = $this->list_dir($sub_dir_list[$i][$j]);
if ( !$l) return $this->result = $result; // If an error occurs, stop returning the existing results immediately
$l = $l["list"];
/* n each child Statistics of files, directories, and other items in the directory, m is the current processing sequence number */
for ($m=0,$n=count($l);$m<$n;$m++) {
if (count($result)>=$limit) return $this->result = $result; // If the required number has been reached, return
if ($l[$m]["type"]== 0) $sub_dir_list[$i+1][] = $l[$m]["locate"].$l[$m]["name"]; // Collect the next level subdirectory information
$ o = $l[$m]["type"];
if ($o!=$seek_type&&($seek_type==1||$seek_type==0)) continue; // Ignore items that do not meet the requirements
elseif ($o==-1&&$seek_type==-1) continue;
if (!preg_match($pattern, $l[$m]["name"])) continue; // Ignore Items that match the regular expression
$result[] = $internal ? $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---- The specified object does not exist
if (is_dir($path)) {
$path = str_replace("", "/", realpath($path));
$path = substr($path, -1)=="/ " ? $path : $path."/";
$sub_list = array(array($path));
for ($i=0;$iif (!isset($sub_list[$i])) break; // Explore to the end and get all the subtitles in the directory Directory list, convenient for deleting directories after deleting files
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); // Delete every file in the directory
}
}
}
for($i=count($sub_list)-1;$i>=0;$i--) // Reverse delete directory
for ($j=0,$k= count($sub_list[$i]);$j<$k;$j++) //Delete each subdirectory until the specified directory
if (!@rmdir($sub_list[$i][$j])) return $this->error_occur(0x0005, $sub_list[$i][$j]); // ----0x0005---- Directory does not have permission to delete
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---- The file has no permission to delete
}

function generate_realpath($path="") {
if ($path==""||!is_string($path)) return $this->error_occur(0x0007, $path); / / ----0x0007---- Path parameter error
$path = preg_replace("/(?|]/", "", str_replace( "", "/", $path)); // Canonical symbols for multiple possibilities in paths
if (substr($path,1,1)==":") return $path; // !! !! Absolute path for Win32 platform
elseif (substr($path,0,1)=="/") return substr(realpath("."), 0, 2).$path; // !!! ! Absolute path conversion under Win32 platform
else {
if (substr($path,-1)=="/") $path = substr($path,0,-1); // Clear the end Possible / symbols
$path = preg_replace("//{2,}/", "/", $path); // Simplify /// similar connected symbols into one
$path = explode("/", $path); // Split 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; // Ignore useless relative path addresses . and .... etc.
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---- Unable to create directory
}
}
return true;
}

function verify_file($src="",$dst="",$internal=true) {
if (!file_exists($src)|| !is_file($src)) return $this->error_occur(0x000A, $src); // ----0x000A---- The specified object is not a file
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&&!$internal) { // For files larger than 1MB, if precise checking is not required, jump Pass
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 = ""; // 记录现行实际存在的目录 
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;$iif (!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); // ----0x0009---- 文件移动失败 




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---- 源文件无权读取 
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;$iif (!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; // 对移动目标目录的上层目录,不予考虑删除 
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); 



?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/317078.htmlTechArticle复制代码 代码如下: ?php error_reporting(2047); /* *ClassIO(SNakeVil完成03.25.04)(v1.0.0.0) * *[说明] *本类用于对文件系统的处理。 * *[功能] *****list_dir(...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn