This article mainly introduces php operations on folders, including traversing and printing all files in the specified directory and calculating the file size to spaces, and deduplication, if you are interested, you can refer to the first operation you have shared:
Traverse and print all objects in the specified directory
<? Php // function: Traverse and print all files in the specified directory. function scan_dir ($ dir_name, $ dir_flag = 1) {static $ FILE_COUNT = 1; // the initial value of the number of recorded files is 1. the directory name is not recorded in $ FILE_COUNT --; // The scan_dir () function minus 1 @ $ dir_handle = opendir ($ dir_name) each time it is called ); // suppress the display of error information to facilitate custom error display if (! $ Dir_handle) die ("directory opening error! "); While (false! = ($ Filename = readdir ($ dir_handle) // when the file name is '0', readdir returns FALSE to determine whether the returned values are incomplete {$ flag = $ dir_flag; // Odd is_dir ($ filename )! $ Filename: the path must be located! Returns false if ($ filename! = '.' & $ Filename! = '.. ') {$ FILE_COUNT ++; // The current path and the upper-level path while ($ flag> 0 & -- $ flag) are not recorded. // The negative number is still true echo ''; if (is_dir ($ dir_name. $ filename) // determines whether it is a directory {echo''. "". $ Filename ."
"; Scan_dir ($ dir_name. $ filename. '/', $ dir_flag + 1); // $ dir_flag indicates the directory tree hierarchy} else {echo" ". $ filename ."
";}} Closedir ($ dir_handle); // Close the directory handle echo" total number of files: ". $ FILE_COUNT .'
';} Scan_dir ('d:/AppServ/www/mytest/shopping/payment/');/* conclusion: 1, add '/' at the end of the path directory to connect to the file name string to form a new path string 2. directory traversal is a tree Issue 3, is_dir () the first parameter of the function must be a recognizable PATH. Otherwise, a false value will be returned because the directory does not exist. it is easy to misunderstand that the "This is not a directory" error is returned to form a logical error 4, note the current path '. 'and upper-level path '.. otherwise, an endless loop occurs during recursion until the memory is exhausted. 5. the dir_flag variable is used to record the depth of the tree to facilitate hierarchical output of directories * // * Less than 1. the output layout is incomplete. 2. the logic and presentation layer are not clear! 3. process-oriented */
##### Single directory #####
// List directory content $ dir do not add '/' function list_files ($ dir) {if (is_dir ($ dir) {if ($ handle = opendir ($ dir )) {while ($ file = readdir ($ handle ))! = False) {if ($ file! = "." & $ File! = "..." & $ File! = "Thumbs. db") {echo ''. $ file .'
'. "\ N" ;}} closedir ($ handle) ;}} list_files ('d:/AppServ/www/mytest/shopping/payment ');
The second PHP operation on the file:Obtain the folder size calculation file
<? Php function getDirSize ($ dir) {$ handle = opendir ($ dir); while (false! ==( $ FolderOrFile = readdir ($ handle) {if ($ FolderOrFile! = "." & $ FolderOrFile! = ".. ") {If (is_dir (" $ dir/$ FolderOrFile ") {$ sizeResult + = getDirSize (" $ dir/$ FolderOrFile ");} else {$ sizeResult + = filesize ("$ dir/$ FolderOrFile") ;}} closedir ($ handle); return $ sizeResult ;} // function getRealSize ($ size) {$ kb = 1024; // Kilobyte $ mb = 1024 * $ kb; // Megabyte $ gb = 1024 * $ mb; // Gigabyte $ tb = 1024 * $ gb; // Terabyte if ($ size <$ kb) {return $ size. "B";} else if ($ size <$ mb) {return round ($ size/$ kb, 2 ). "KB";} else if ($ size <$ gb) {return round ($ size/$ mb, 2 ). "MB";} else if ($ size <$ tb) {return round ($ size/$ gb, 2 ). "GB";} else {return round ($ size/$ tb, 2 ). "TB" ;}} echo getRealSize (getDirSize ('d:/AppServ/www/mytest/'); // get byte function dirsize ($ dir) {$ handle = opendir ($ dir); $ size = 0; while ($ file = readdir ($ handle) {if ($ file = ". ") | ($ file = ".. ") continue; if (is_dir (" $ dir/$ file ") $ size + = dirsize (" $ dir/$ file "); else $ size + = filesize ("$ dir/$ file");} closedir ($ handle); return $ size ;} $ big = dirsize (dirname ($ _ SERVER ['script _ filename']). "/"); echo '('. $ big. 'bytes )';
The above is the basic php operations on folders. I hope it will help you learn.