File system basic Operation Class _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
Error_reporting (2047);

/*
* Class IO (Snakevil completed 03.25.04) (v1.0.0.0)
*
* [Description]
* This class is used for processing the file system.
*
* [function]
* * * * * LIST_DIR ($dir _path);
* Reads the contents of the specified directory and returns an array of contents.
* $dir _path string, specifying directory path
* Returns False if error is returned.
* 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);
* Search for matching files and directories in the corresponding directories and subdirectories of a given level according to the regular expression conditions.
* $pattern Regular expressions that comply with PERL compliant standards, without adding//and adding the system yourself
* $seek _type have-1 0 13 possible values, 0 folders only, 1 files only, 12 are included
* $sub _dir numeric value, search subdirectory depth, specified directory does not count, recommended not more than 5
* $interal Boolean, returns the details of the search results, or returns only the file name, type, and directory in which it was found
* $limit numeric value, limit search results, avoid excessive waste of system resources
* Returns False if error is returned.
* Array (
* Array (
* "Name", "Locate", "type"
* [, "Size", "last_access", "Last_change", "last_modify"]
* ),
* ......
* )
* ********
* ********
* * * * * DELETE ($path);
* Delete the specified object, file or folder-including non-empty folders containing subdirectories and files.
* $path string specifying the content path to be deleted, the file or directory can be
* If error is interrupted at error, return FALSE, otherwise true
* ********
* ********
* * * * * MAKE_DIR ($path);
* Create any folder, relative or absolute path can be, deep build can also.
* $path string, the final directory path to be established
* Returns TRUE if error returns false
* ********
* ********
* * * * * * Verify_file ($SRC, $DST, $interal);
* Compare two files using the MD5 algorithm.
* $SRC string, source file path
* $DST string, destination file path
* $interal boolean value, for files larger than 1M, can be set to FALSE to eliminate MD5 inspection steps and reduce server burden
* Returns TRUE if an error returns false
* ********
* ********
* * * * Copy ($src _path, $dst _path);
* To copy any folder, file, relative or absolute path can be, the file copy is done after the validation, check whether error data error.
* $SRC _path string specifying the source content path to be copied, the file or directory can be
* $DST _path String, specifies the destination content path to be copied, the file or directory can be, the nature is determined by the $SRC _path, can be $src _path lower directory
* Returns TRUE if an error returns false
* ********
* ********
* * * * * Move ($src _path, $dst _path);
* For any folder, file to move, relative or absolute path can be, the file movement is done after the validation, check whether error data errors.
* $SRC _path string specifying the source content path to be moved, the file or directory can be
* $DST _path String, specify the destination content path to move, file or directory can be, nature determined by $src _path, can be $src _path lower directory
* Returns TRUE if an error returns false
*
* [Copyright]
* Wind and rain Ming and Qing dynasties (SNAKEVIL@51JS, SNAKEVIL@BU) Independent design completed, All rights reserved.
* Free to use, but do not keep the following text, 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----An error occurred, but the cause of the error is unknown
if (Is_int ($error _id)) $this->error_id = $error _id; Get error number
$this->error_related = $error _related;
return false; False when error occurs to facilitate further processing
}

function List_dir ($dir _path= ".") {
if (!is_dir ($dir _path)) return $this->error_occur (0x0001, $dir _path); ----0X0001----The specified directory does not exist
if (! $dir _handle= @opendir ($dir _path)) return $this->error_occur (0x0002, $dir _path); ----0X0002----The specified directory does not have permission to read
$result = Array (
"Count" = = Array ("Files" = 0, "dirs" = 0, "size" = 0),
"List" = = Array ()
);
while (false!== ($file _handle=readdir ($dir _handle)) {//Use!== to prevent processing of files, directories with name 0 or false
if ($file _handle== "." | | $file _handle== "..") Continue Ignore system-specific two folders
$temp = Str_replace ("\", "/", Realpath ($dir _path));
$temp = substr ($temp,-1) = = "/"? $temp: $temp. " /";
$temp = Array ($temp, $file _handle);
$file _handle = $temp [0]. $temp [1]; Get absolute Address
$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://!!!! Given the Win32 platform, ignoring content that is neither a file nor a directory
$temp ["type"] =-1;
}
$result ["list"] = $temp;
}
Closedir ($dir _handle);
Unset ($dir _handle, $file _handle, $temp);
Clearstatcache (); Clearing the file system cache
return $this->result = $result;
}

function Seek_file ($pattern = ". *", $dir _path= ".", $seek _type=1, $sub _dir=0, $interal =false, $limit =100) {
/* Specification of 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));
$interal = (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 layer of the subdirectory hierarchy
$result = Array ();
/* I currently process the subdirectory hierarchy, 0 for the specified directory layer, that is, to process only one directory */
for ($i =0; $i <= $sub _dir; $i + +) {
if (!isset ($sub _dir_list[$i])) return $this->result = $result; If a level subdirectory is not set, there is no directory in the actual directory system and returns
/* k per subdirectory hierarchy of sub-directory statistics, j current processing sequence number */
For ($j =0, $k =count ($sub _dir_list[$i]), $j < $k, $j + +) {//per-layer sub-directory quantity processing
$l = $this->list_dir ($sub _dir_list[$i] [$j]);
if (! $l) return $this->result = $result; An error occurs and immediately stops returning the existing results
$l = $l ["List"];
/* n each subdirectory in the file, directory, other item statistics, M is the current processing ordinal */
For ($m =0, $n =count ($l), $m < $n; $m + +) {
if (count ($result) >= $limit) return $this->result = $result; If the requested 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 of 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 do not conform to regular expressions
$result [] = $interal? $l [$m]: Array ("name" + $l [$m] ["name"], "locate" and "[$l] [" Locate "]," type "and" [$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----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; $i if (!isset ($sub _list[$i])) break; Explore to the very end, get a list of all subdirectories in this directory, easy to delete after file delete directory
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 a directory
}
}
}
For ($i =count ($sub _list)-1; $i >=0; $i-)//Reverse Back 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----file does not have 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)); Symbol of multiple probabilities in canonical path
if ($path, substr) = = ":") return $path; // !!!! Absolute path to the Win32 platform
ElseIf (substr ($path, 0,1) = = "/") return substr (Realpath ("."), 0, 2). $path; // !!!! Absolute path conversion under the Win32 platform
else {
if (substr ($path, -1) = = "/") $path = substr ($path, 0,-1); Clear the end of possible/symbol
$path = Preg_replace ("//{2,}/", "/", $path); simplifies///such as a similar linked symbol to a
$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; Ignores the useless relative path address. And.... such as
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----Cannot create a directory
}
}
return true;
}

function Verify_file ($src = "", $dst = "", $interal =true) {
if (!file_exists ($SRC) | |! Is_file ($SRC)) return $this->error_occur (0x000A, $SRC); ----0x000A----Specified object non-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&&! $interal) {//For files greater than 1MB, if the exact check is not required, skip
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 = ""; Record the actual existing directory
if (! $this->make_dir ($dst _path)) return false; Failed to build directory
$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 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; Prevent a dead loop if the parent directory is replicated to the subordinate directory
$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; Built for each subdirectory
} else {//copy each file
if ($this->verify_file ($o, $p)) continue; If the target is exactly the same as the source, no longer replicates
if (!copy ($o, $p) | |! $this->verify_file ($o, $p)) return $this->error_occur (0x0009, $o); ----0x0009----File move failed
}
}
}
}
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----Source file does not have permission to read
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 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; Do not consider deleting the upper directory of the Moving target directory
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);
}
}
}
?>

http://www.bkjia.com/PHPjc/317078.html www.bkjia.com true http://www.bkjia.com/PHPjc/317078.html techarticle Copy the code as follows: PHP error_reporting (2047);/* *classio (Snakevil complete 03.25.04) (v1.0.0.0) * *[Description] * This class is used for processing the file system. * *[function] *****list_dir (...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.