The project needs to write a function that traverses all the files in the specified directory, and the subdirectories in that directory are traversed. Output The property information of the file and store it.
Think of the need, is not a ls-al command, to achieve the relevant properties of the good, plus a traversal OK.
In the process of the project, it is easy to manipulate, using JSON format storage, but also found some problems. Please record the problem and code for easy reference.
<?php define (' Indexformat ', "Dir,name,size,perms,ower,group,ctime,mtime,atime,suffix");
Define (' Indextxt ', ' data/index.txt ');
Define (' Indexjson ', ' Data/index.json ');
Date_default_timezone_set (' Asia/hong_kong ');
if (file_exists (Indextxt)) {unlink (indextxt);
} $dir = './';
Getindexfile ($dir); /* Get index file * @filename indextxt * @dir String * */function Getindexfile ($dir, $whitelist = ")
{$string = ';
$dir = Trim ($dir);
$dir = Realpath ($dir); $dir = $dir. "
//";
if (Is_file ($dir)) {putindexfile ($dir);
}else{Putindexfile ($dir);
$oDir = @opendir ($dir); while ($fileName = Readdir ($oDir)) {if ($fileName!= '. ' && $fileName!= ' ... ')
{if (Is_file ($dir. $fileName)) {putindexfile ($dir. $fileName);
}elseif (Is_dir ($dir. $fileName)) {getindexfile ($dir. $fileName); }} if (!file_exists (Indextxt)) {REturn false;
}//$data = Json_encode (Getindexfromfile ());
File_put_contents (Indexjson, $data);
return true; }/* Get Index File * @filename file.index * * */function Putindexfile ($file) {if (!file_exists) ($fil
e)) {return false;
$format = Explode (', ', Indexformat);
$string = "";
foreach ($format as $key => $value) {if ($key!== 0) {$string. = "\ T";
$string. = Getfileattr ($file, $value);
$string. = "\ n";
File_put_contents (Indextxt, $string, file_append);
}/* * Get index string from index file * @return Array () */function getindexfromfile ($flag = ') {
if (!file_exists (Indextxt)) {return false;
} $arr = file (indextxt);
$format = Explode (', ', Indexformat);
$result = Array ();
if (!empty ($flag)) {$key = Array_search ($flag, $format);
if ($key = = False) {return false;
foreach ($arr as $str) { $tmp = Explode ("", Trim ($STR));
$result [$flag] = $tmp [$key];
}}else{foreach ($arr as $str) {$tmp = Explode ("", Trim ($STR));
foreach ($format as $key => $value) {$result [$value] = $tmp [$key];//tested this operation more time-consuming, about 0.7s of the appearance, use caution!
}} return $result; }/* Get file attributes * @var $file * @var $flag * @return String/function getfileattr ($f
Ile, $flag) {if (!file_exists ($file)) {return false;
Switch ($flag) {case ' dir ': if (Is_file ($file)) return dirname ($file);
Return Realpath ($file);
Break
Case ' name ': if (Is_file ($file)) return basename ($file);
Return '-';
Break
Case ' size ': if (Is_file ($file)) return filesize ($file);
Return '-';
Break
Case ' perms ': Return substr (sprintf ('%o ', Fileperms ($file)),-4);;
Break Case ' ower ': Return Fileowner ($file);
Break
Case ' group ': Return filegroup ($file);
Break
Case ' CTime ': Return Filectime ($file);
Break
Case ' Mtime ': Return Filemtime ($file);
Break
Case ' Atime ': Return Fileatime ($file);
Break Case ' suffix ': if (Is_file ($file)) return substr ($file, Strrpos ($file, '. ')
+1);
Return '-';
Break
Default:return false;
Break
} * * Get file size human readable */function Getfilesizeformat ($file) {if (!is_file ($file)) {
Return '-';
$flags = Array (', ' K ', ' M ', ' G ', ' T ');
for ($i = 0; $size >= 1024 && $i < 4; $i + +) $size/= 1024;
Return round ($size, 2). $flags [$i];
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.