File system basic Operation Class _php skill

Source: Internet
Author: User
Tags abs explode file copy md5 numeric value parent directory win32
Copy Code code as follows:



<?php


Error_reporting (2047);





/*


* Class IO (snakevil complete 03.25.04) (v1.0.0.0)


*


* [Description]


* This class is used for processing file systems.


*


* [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 any errors are returned, or returns


* 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 hierarchy, based on regular expression conditions.


* $pattern regular expressions that conform to PERL compliant standards without adding//, adding the system itself


* $seek _type has-1 0 13 possible values, 0 folders only, 1 files only,-12 are included


* $sub _dir numeric value, search the subdirectory depth, the specified directory does not count, not recommended more than 5


* $interal Boolean value, returns the details of the search result, or returns only the file name, type and directory


* $limit numeric values, search results limit, avoid excessive waste of system resources


* Returns False if any errors are returned, or returns


* Array (


* Array (


* "Name", "Locate", "type"


* [, "Size", "last_access", "Last_change", "last_modify"]


* ),


* ......


* )


* ********


* ********


* * * * * * * * * * ($PATH);


* Deletes the specified object, file, or folder-a Non-empty folder that contains subdirectories and files.


* $path string specifying the content path to delete, and the file or directory can be


* If there is an error interrupted at the error, return FALSE, otherwise return 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


* Return TRUE if error returns false


* ********


* ********


* * * * * * * * * * * Verify_file ($SRC, $DST, $interal);


* Use the MD5 algorithm to compare whether two files are the same.


* $SRC string, source file path


* $DST string, destination file path


* $interal Boolean value, can be set to FALSE for larger than 1M files to eliminate MD5 inspection steps and reduce server burden


* Returns TRUE if error returns false


* ********


* ********


* * * * * * * Copy ($src _path, $dst _path);


* Any folder, file replication, relative or absolute path can be completed after the completion of the file copy, check whether the error data errors.


* $SRC _path string specifying the source content path to be copied, and the file or directory can


* $DST _path string specifying the path of the destination content to be replicated, the file or directory, the nature of which is determined by the $SRC _path, and can be $src _path downlevel directory


* Returns TRUE if error returns false


* ********


* ********


* * * * * * Move ($src _path, $dst _path);


* to any folder, file movement, relative or absolute path can be completed after the completion of the file movement will be tested, check whether error data errors.


* $SRC _path String that specifies the path of the source content to be moved, and the file or directory can


* $DST _path string specifying the path of the destination content to be moved, the file or directory, the nature of which is determined by the $SRC _path, and can be $src _path lower directory


* Returns TRUE if error returns false


*


* [Copyright]


* Wind and rain in Ming and Qing (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:<a href= "http://www.snakevil.com/" target= "_blank" >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----An error occurred, but the reason for the error is unknown


if (Is_int ($error _id)) $this->error_id = $error _id; Get error number


$this->error_related = $error _related;


return false; Returns False when an error occurs to facilitate further processing


}





function List_dir ($dir _path= ".") {


if (!is_dir ($dir _path)) return $this->error_occur (0x0001, $dir _path); ----0X0001----Specified directory does not exist


if (! $dir _handle= @opendir ($dir _path)) return $this->error_occur (0x0002, $dir _path); ----0X0002----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 files, directories with names of 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],


The "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 (); Purge 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 processing subdirectory hierarchy, 0 is the specified directory layer, that is, only one directory processing


for ($i =0; $i <= $sub _dir; $i + +) {


if (!isset ($sub _dir_list[$i]) return $this->result = $result; If a layer of subdirectories is not set, the actual directory system does not have a directory, return


/* k per subdirectory Level neutron directory statistics, j current processing serial number * *


For ($j =0, $k =count ($sub _dir_list[$i]); $j < $k; $j + +) {//Per-level subdirectory number processing


$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 every subdirectory of files, directories, other items statistics, 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 number of requests has been reached, return


if ($l [$m] ["type"]==0) $sub _dir_list[$i +1][] = $l [$m] ["locate"]. $l [$m] ["name]"; Gather 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" => $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; $i <count ($sub _list); $i + +) {//Use COUNT ($SUB _list) to dynamically determine the length, which may have an indefinite loop


if (!isset ($sub _list[$i]) break; Explore to the very end, get the directory of all subdirectories list, easy to delete files after deleting the 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 under 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 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 ("/(? <!^w) [:*?") <>|] /"," ", Str_replace (" \ ","/", $path)); Symbols for multiple probabilities in canonical paths


if (substr ($path, 1,1) = = ":") return $path; // !!!! Absolute path of 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 possible/symbol


$path = Preg_replace ("//{2,}/", "/", $path); Simplifies///, such as similar concatenated symbols, into 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 unwanted relative path address. And.... Wait


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----Specify 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 larger than 1MB, skip if exact check is not required


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 <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; To prevent a dead loop if you copy the parent directory 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; Set up for each subdirectory


else {//copy every file


if ($this->verify_file ($o, $p)) continue; If the target is exactly the same as the source, no longer replicate


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 <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; Do not consider deleting the top-level directory of the moving target directory


ElseIf (! @rmdir ($sub _list[$i [$j]) return 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);


}


}


}


?>


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.