Implementing file Management System with PHP
/**
* @]class name[= IO
* @]class uri[= System.IO
* @]purpose[=
* This class is used to process the file system
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]version[= 1.1.1
* @]create[= 17:13 2004-3-25
* @]modifications[=
* 4:04 2004-3-30 "
* + fix some bugs in the Generate_path () method
* + Re-design Method No_comment ()
* 4:32 2004-3-29
* + Simplified Method List_dir () return value [
* + Add Method File_info () Get file or directory information
* 5:35 2004-3-28
* + Finishing Optimization algorithm
* 7:31 2004-3-27
* + Abstract error handling as base class PB
* + Add Method no_comment () Delete C specification Note in file
* @]see[=
*/
Class IO extends Snkclass
var $result; Operation returns the result, if the method return value is mixed, the result of a successful operation can be obtained here
var $exec _cmd; Execution method, temporarily not applied to
var $exist _dir; The last directory that existed when the directory was created, now used for copy () and move ()
var $buffer _size; File read buffer size, based on service application scale and server configuration modifications, recommended default values
function IO ()
Parent::snkclass ();
$this->result = Array ();
$this->exec_cmd = "";
$this->exist_dir = "";
$this->buffer_size = 8192;
return $this;
}
/**
* @]method name[= List_dir ()
* @]purpose[=
* Reads the contents of the specified directory and returns an array of contents
* @]parameter[=
* String $dir _path Specifies the directory path, which defaults to the current directory
* @]return[= Mixed Error returns false, otherwise returns
* Array (
* Array ("name", "Location", "type"),
* ......
* )
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function List_dir ($path = ".")
if (!is_dir ($path)) return $this->error_occur (0x000b, __function__);
if (!is_readable ($path)) return $this->error_occur (0x0002, $path);
$DH = @opendir ($path);
$result = Array ();
$path = Realpath ($path);
if ($path [strlen ($path) -1]!=directory_separator) $path. = Directory_separator; Guaranteed directory absolute address with directory delimiter
while (false!== ($fh =readdir ($DH))) {//Use!== to prevent processing of files, directories with name 0 or FALSE
if ($fh = = "." | | $fh = = "..") Continue Ignore system-specific folders
$i = $path. $fh; Get absolute Address
$t = Array (
"Name" = $fh, V u,
"Location" and $i,
"Type" = Is_file ($i)? 1: (Is_dir ($i)? 0:-1)
);
$result [] = $t;
}
Closedir ($DH);
Unset ($DH, $fh, $t, $i);
Clearstatcache (); Clearing the file system cache
return $this->result = $result;
}
/**
* @]method name[= File_info ()
* @]purpose[=
* Gets the properties of the specified file or directory
* @]parameter[=
* String $dir _path Specifies the directory path, which defaults to the current directory
* @]return[= Mixed Error returns false, otherwise returns
* Array ("name", "Location", "type", "Size", "Access", "Change", "Modify", "read", "write"),
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function File_info ($path = ".")
$path = Realpath ($path);
if (! $path) return $this->error_occur (0x000A, __function__);
$result = Array (
"Name" = substr ($path, Strrpos ($path, Directory_separator) +1),
"Location"-$path, Fhhc,~|5
"Type" = Is_file ($path)? 1: (Is_dir ($path)? 0:-1),
"Size" = FileSize ($path),
"Access" = Fileatime ($path),
"Modify" = Filemtime ($path),
"Change" and Filectime ($path),
"Read" = Is_readable ($path),
"Write" = Is_writeable ($path)
);
Clearstatcache ();
return $this->result = $result;
}
/**
* @]method name[= seek file ()
* @]purpose[=
* Search for matching files and directories in corresponding directories and subdirectories of a given level according to regular expression conditions
* @]parameter[=
* String $pattern a PERL-compliant regular expression that indicates a search match requirement, adds/^ $/, and defaults to. *
* String $path the directory path to search, default to the current path
* Enum $seesk _type has-1 0 13 possible values, 0 folders only, 1 files only, 12 are included, default is 1
* int $sub _dir Search subdirectory depth, specify the directory does not count, recommended not more than 5, the default is
* Limit $limit search result limits to avoid excessive waste of system resources by default of 100
* @]return[= Mixed Error returns false, otherwise
* Array (
* Array (
* "Name", "Locate", "type"
* ),
* ......
* )
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function Seek_file ($pattern = ". *", $path = ".", $seek _type=1, $sub _dir_level=0, $limit =100)
/* Check parameter values */
$is _error = $seek _type!=1 && $seek _type!=0 && $seek _type!=-1;
$is _error = $is _error && (!is_int ($sub _dir_level) | | $sub _dir_level < 0);
$is _error = $is _error && (!is_int ($limit) | | $limit < 1);
if ($is _error) return $this->error_occur (0x000b, __function__);
unset ($is _error);
$result = Array ();
/* Array () = = FALSE, so you need to use = = = *
if (false=== $i = $this->list_dir ($path)) return FALSE; If the directory cannot be enumerated, return
For ($j =0, $k =count ($i), $j < $k; $j + +) {
if ($i [$j] ["type"]==-1) continue; For non-directory non-file items, skip
if ($i [$j] ["type"]==0&& $sub _dir_level) {//If you need to search the underlying directory
if (false=== $l = $this->seek_file ($pattern, $i [$j] [location], $seek _type, ($sub _dir_level-1), $limit)) return FALSE;
$result = Array_merge ($result, $l); Add the underlying directory search results
}
if ($seek _type+ $i [$j] ["Type"]==1| |! Preg_match ("/^". $pattern. " $/", $i [$j] [" name "])) continue; If you do not search for the current type, skip
$result [] = $i [$j];
if (count ($result) >= $limit) {//truncate exceeds the required length, leave enumeration
Array_splice ($result, $limit);
Break
}
}
Unset ($i, $j, $k, $l);
return $this->result = $result;
}
/**
* @]method name[= Delete ()
* @]purpose[=
* Delete the specified object, file or folder-including non-empty folders containing subdirectories and files
* @]parameter[=
* String $path Specifies the content path to be deleted, and the file or directory can be
* @]return[= Boolean error returns FALSE, otherwise TRUE
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function Delete ($path = "") {
$path = Realpath ($path);
if (! $path) return $this->error_occur (0x000A, __function__);
if (!is_dir ($path)) {
if (@unlink ($path)) return TRUE; File deletion succeeded
return $this->error_occur (0x0004, $path);
} else {
if (false=== $i = $this->list_dir ($path)) return FALSE; Cannot enumerate directories
For ($j =0, $k =count ($i), $j < $k, $j + +)
if (! $this->delete ($i [$j] [location])) return FALSE; Error deleting directory Contents
Unset ($i, $j, $k);
return TRUE;
}
}
/**
* @]method name[= Generate_path () =l
* @]purpose[=
* Get an absolute address for existing or nonexistent files and directories
* @]parameter[=
* String $path the file, directory existing relative, absolute address to get the address
* @]return[= string Gets the address
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function Generate_path ($path = "") {
$i = "/" ==directory_separator? "\\" : "/"; Unified Directory Separator
$path = Str_replace ($i, Directory_separator, Strval ($path));
if ($path [strlen ($path) -1]!=directory_separator) $path. = Directory_separator;
$i = Strpos ($path, directory_separator); Get the location of the first directory separator in the path
$ext = substr ($path, $i + 1);
$path = substr ($path, 0, $i + 1);
if ($i =realpath ($path)) $path = $i; Get the basic path
else {
$ext = $path. $ext;
$path = Realpath (".");
}
if (strlen ($ext)) {//For remaining content processing
$ext = Preg_replace ("/[\:\*\?\" \<\>\|] /"," ", Explode (Directory_separator, $ext));
Array_pop ($ext);
$path = Explode (Directory_separator, $path); Create a catalog layer axis
if ($path [count ($path) -1]== ") Array_pop ($path);
while (count ($ext)) {
$i = Array_shift ($ext);
if ($i = = "..." &&count ($path) >1) Array_pop ($path);
ElseIf (""!=str_replace (".", "", $i)) $path [] = $i;
}
$path = Implode (Directory_separator, $path);
}
Unset ($ext, $i);
return $path;
}
/**
* @]method name[= Make_dir ()
* @]purpose[=
* Create any folder, relative or absolute path can be, deep build can also
* @]parameter[=
* String $path The final directory path to be established
* @]return[= Boolean error returns FALSE, otherwise TRUE
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/ )
function Make_dir ($path = "") {
$i = Explode (Directory_separator, $this->generate_path ($path)); Generate directory path
$path = Array_shift ($i);
For ($j =0, $k =count ($i), $j < $k; $j + +) {
$path. = Directory_separator. $i [$j];
if (!is_dir ($path)) {
if ($this->exist_dir== "") $this->exist_dir = $path; Record the last existing directory path
if (! @mkdir ($path)) return $this->error_occur (0x0003, substr ($path, 0, Strrpos ($path, Directory_separator)));
}
}
if ($this->exist_dir== "") $this->exist_dir = $path;
return TRUE;
}
/**
* @]method name[= Verify_file ()
* @]purpose[=
* Compare two files using the MD5 algorithm
* @]parameter[=
* String $src source file path
* String $DST destination file path
* Boolean $interal for more than 1MB files, set FALSE to omit MD5 inspection steps, reduce server burden
* @]return[= Boolean error returns FALSE, otherwise TRUE
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function Verify_file ($src = "", $dst = "", $interal =true) {
if (!is_file ($SRC) | |! Is_file ($DST)) return $this->error_occur (0x000b, __function__);
if (!is_readable ($SRC)) return $this->error_occur (0x0006, $SRC);
if (!is_readable ($DST)) return $this->error_occur (0x0006, $DST);
$i = FileSize ($SRC);
if (filesize ($DST)! = $i) {//File size varies
Unset ($i);
return FALSE;
}
if ($i >1024*1024*1024&&! $interal) {//For 1MB files, if the exact check is not required, skip
Unset ($i);
return TRUE;
}
Unset ($i);
if (Md5_file ($SRC)!=md5_file ($DST)) return FALSE; Document MD5 validation Non-conformance, content is not the same
return TRUE;
}
/**
* @]method name[= Copy ()
* @]purpose[=
* To copy any folder, file, relative or absolute path can be, the file copy is done after the validation, check whether error data error
* @]parameter[=
* String $src _path Specifies the source content path to be copied, the file or directory can be
* String $DST _path 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
* @]return[= Boolean error returns FALSE, otherwise TRUE
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function copy ($src = "", $dst = "", $sub =false) {
if (! $src =realpath ($SRC)) return $this->error_occur (0x000b, __function__);
$DST = $this->generate_path ($DST);
if (Is_dir ($SRC)) {//Process directory
/*
* Description of the algorithm:
* Originally intended to use a very simple recursive algorithm, encounter God kill God, Encounter Magic Chopper, later found a problem: if the target path
* What about the descendant path of the source path? This algorithm will continue to detect ...
* The $this->exist_dir attribute is added to record the portion of the target path that is present in this case. So the new question
* The question comes out: how do I save this property?
* Integrate the entire function into the $this->copy () method, it is necessary to record in this function
* Changes, you need another effective way to block changes in each operation.
* As a workaround, I use the hidden parameters $sub, this parameter anyway, as long as the algorithm does not change, always in the parameter table the most
* The latter one. As a result, the method begins to become unstable, but there is no way to expect the programmer to deliberately destroy itself.
* When called externally, the $this->exist_dir is written because the default is FALSE. Internal recursion, dominant TRUE,
* This property is guaranteed to be valid.
*/
if (!is_readable ($SRC)) return $this->error_occur (0x0002, $SRC);
if ($dst [strlen ($DST) -1]!=directory_separator) $dst. = Directory_separator;
if (true=== $sub && $src = = $this->exist_dir) return TRUE; Source path is the target path for the record
if (true!== $sub) $this->exist_dir = ""; Records the directory path that exists in the destination directory path before the directory was created
if (! $this->make_dir ($DST)) return FALSE; Create a Directory
if (false=== $i = $this->list_dir ($src)) return FALSE; Error reading Directory
For ($j =0, $k =count ($i), $j < $k, $j + +) if (! $this->copy ($i [$j] [location], $dst. $i [$j] ["name"],true)) return FALSE;
Unset ($i, $j, $k);
RETURN TRUE;
} else {
if (!is_readable ($SRC)) return $this->error_occur (0x0006, $SRC);
if ($this->verify_file ($SRC, $DST)) return TRUE;
if (!copy ($SRC, $DST)) return $this->error_occur (0x0007, $DST);
if (! $this->verify_file ($SRC, $DST)) {
@unlink ($DST); Copy file failed to delete new file
return $this->error_occur (0x0007, $DST);
}
return TRUE;
}
}
/**
* @]method name[= Move ()
* @]purpose[=
* For any folder, file to move, relative or absolute path can be, the file will be checked after the move is completed, check if error data error www.hidianying.cn
* @]parameter[=
* String $src _path Specifies the source content path to be moved, the file or directory can be
* String $DST _path Specifies the destination content path to be moved, the file or directory can be, the nature is determined by the $SRC _path, can be $src _path lower directory
* @]return[= Boolean error returns FALSE, otherwise TRUE
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function Move ($SRC = "", $dst = "", $sub =false) {
if (! $src =realpath ($SRC)) return $this->error_occur (0x000b, __function__);
$DST = $this->generate_path ($DST);
if (Is_dir ($SRC)) {//Process directory
if (!is_readable ($SRC)) return $this->error_occur (0x0002, $SRC);
if ($dst [strlen ($DST) -1]!=directory_separator) $dst. = Directory_separator;
if (true=== $sub && $src = = $this->exist_dir) return TRUE;
if (true!== $sub) $this->exist_dir = "";
if (! $this->make_dir ($DST)) return FALSE;
if (false=== $i = $this->list_dir ($src)) return FALSE;
For ($j =0, $k =count ($i), $j < $k, $j + +) if (! $this->move ($i [$j] [location], $dst. $i [$j] ["name"],true)) return FALSE;
Unset ($i, $j, $k);
if (False===strpos ($this->exist_dir, $SRC))
if (! @rmdir ($SRC)) return $this->error_occur (0x0004, $SRC); To the upper directory of the non-target directory, delete
return TRUE;
} else {
if (!is_readable ($SRC)) return $this->error_occur (0x0006, $SRC);
if ($this->verify_file ($SRC, $DST)) return TRUE;
if (!copy ($SRC, $DST)) return $this->error_occur (0x0007, $DST);
if (! $this->verify_file ($SRC, $DST)) {M
@unlink ($DST);
return $this->error_occur (0x0007, $DST);
}
if (! @unlink ($SRC)) return $this->error_occur (0x0006, $SRC); Delete source files
return TRUE;
}
}
/**
* @]method name[= no_comment ()
* @]purpose[=
* Clear the notes of the C specification in the file
This tutorial comes from the 97xxoo tutorial Web (www.97xxoo.org) to see the full tutorial please click: http://www.97xxoo.org/article/1/2008/20081018053.shtml
* @]parameter[=
* String $path Specifies the file to perform the action on
* @]return[= Boolean error returns FALSE, otherwise TRUE
* @]author[= snakevil <51JS,BU,PHPx> (snakevil@qq.com)
* @]see[=
*/
function no_comment ($path = "") {
if (!is_file ($path)) return $this->error_occur (0x000b, __function__);
if (!is_readable ($path)) return $this->error_occur (0x0006, $path);
if (!is_writeable ($path)) return $this->error_occur (0x0007, $path);
if (! $th =tmpfile ()) return $this->error_occur (0x000c, $path); Create a temporary file
$fh = fopen ($path, "r+b");
if (!flock ($FH, LOCK_EX)) {//Lock file
Fclose ($FH);
Unset ($FH);
return $this->error_occur (0x0009, $path);
}
$fbuffer = Fread ($fh, $this->buffer_size*2); File Read buffer
$tbuffer = ""; Temporary file buffers <
$in _DQ = $in _sq = $in _LC = $in _BC = FALSE;
while ($fblen =strlen ($fbuffer)) {//Process raw data
$fstats = feof ($FH);
for ($i =0; $i < $fblen; $i + +) {//Analyze file contents
if (! $fstats && $i +5> $fblen) break; Read the next piece of file when the file is not fully read near the buffer read complete
$j = substr ($fbuffer, $i, 2);
$k = $j [0];
if ($j = = "/*" &&! $in _dq&&! $in _sq&&! $in _lc) {//not in string and line comments, block comments start
$in _BC = TRUE;
$i + +;
} elseif ($j = = "* *" && $in _BC) {//block comment end
$in _BC = FALSE;
$i +=2;
} elseif ($j = = "//" &&! $in _dq&&! $in _sq&&! $in _bc) {//Line comment start
$in _LC = TRUE;
$i + +;
} elseif ($in _lc&& ($k = = "\ r" | | $k = = "\ n")) $in _lc = FALSE; End of line Comment
ElseIf ($j = = "\\\\" | | $j = = "\\\" "| | $j = = "\ \") {//escape character)
$tbuffer. = $j;
$i + +;
Continue
} elseif ($k = = "\" "&&! $in _sq&&! $in _bc&&! $in _lc) $in _DQ =! $in _dq; Double quote string start, end
ElseIf ($k = = "'" &&! $in _dq&&! $in _bc&&! $in _lc) $in _sq =! $in _sq; Single quote string start, end
if ($in _lc| | $in _BC) continue; In the comment, skip
$tbuffer. = $fbuffer [$i];
}
$fbuffer = substr ($fbuffer, $i); Discard the part that has been read
Unset ($i, $j, $k);
if (! $fstats) $fbuffer. = Fread ($fh, $this->buffer_size);
if ($fstats | | Strlen ($tbuffer) >= $this->buffer_size) {//write legitimate data to a temporary file
if (!fwrite ($th, $tbuffer)) {//write failed with insufficient space
Fclose ($th);
Flock ($FH, lock_un);
Fclose ($FH);
Unset ($th, $fh, $in _dq, $in _sq, $in _lc, $in _bc, $i, $j, $k);
return $this->error_occur (0x000d, "");
}
$tbuffer = "";
}
}
Unset ($fbuffer, $tbuffer, $fstats, $in _dq, $in _sq, $in _lc, $in _BC);
Rewind ($FH); Move the file pointer back to the top of the file
Rewind ($th);
$i = $j = "";
$k = 0;
while (!feof ($th)) {//writes temporary file data back to the source file
$i = Fgets ($th, $this->buffer_size);
if ($j = = "") {//Get line breaks for the file system
$j = substr ($i,-2);
if ($j = = "\ r \ n")
ElseIf ($j [1]== "\ r" | | $j [1]== "\ n")
$k = 1;
$j = $j [1];
} else $j = "";
}
if (substr ($i,-$k) = = $j) {
$i = RTrim (substr ($i, 0,-$k), "\ t");
if (strlen ($i)) fwrite ($fh, $i. $j); Clear the right space
else continue;
} else Fwrite ($fh, RTrim ($i, "\ T"));
}
Fflush ($FH); Save, close files
Ftruncate ($FH, Ftell ($FH));
Fclose ($th);
Flock ($FH, lock_un);
Fclose ($FH);
Unset ($i, $j, $k, $fh, $th);
return TRUE;
}
}
/**
* @]error list[=
* 0x0001 specified directory does not exist
* 0x0002 specified directory without Read permission
* 0x0003 specified directory without write permission
* 0x0004 specified directory without delete permission
* 0x0005 specified file does not exist
* 0x0006 Specifies no read permission for the file
* 0x0007 Specifies no write permission for the file
* 0x0008 specified file without delete permission
* 0x0009 specified file cannot be locked
* 0x000A specified object does not exist
* The 0x000b method specifies that the parameter is incorrect
* 0x000c cannot create temporary files
* 0X000D disk space is low
* 0x000e
* 0X000F
* 0x0010
* 0x0011
*
*/
?>
http://www.bkjia.com/PHPjc/845142.html www.bkjia.com true http://www.bkjia.com/PHPjc/845142.html techarticle using PHP to implement the file management system/** * @] Class name[= IO * @]class uri[= System.IO * @]purpose[= * This class is used for processing of file systems * @]author[= Snakevil 51JS,BU,PHPX (snakevil@q...