This morning on the subway to read a chapter on files and folders, just recently understand the Linux file system, feel the access to file properties and Shell commands are very similar, so want to practice at night.
found that the PHP folder function does not seem to provide traversal of all the files under the folder (including subdirectories), so you want to implement one.
When you write, you find that some operations folder functions are not what you think. For example, DirName () takes the path to the folder where the file is located according to the full file path provided, but if you pass in a folder, it gets its parent folder. This is to be understood. BaseName () is also the same reason that the Incoming folder path is the name of the folder. The results returned by these two functions do not end with '/'
<?php $dirPath = '/home/www/test '//directory, ending without '/'//Performing traversal recursion_readdir ($dirPath); /** * @summary repeat times char * @param $char Duplicate characters * @param $times repeat number * @return Returns a string of repeating characters/function Forchar ($char = '-'
, $times =0) {$result = ';
for ($i =0; $i < $times; $i + +) {$result. = $char;
return $result; /** * @summary Recursive Read directory * @param $dirPath directory * @param $Deep = 0 depth, for indentation, without manually setting * @return no/function Recursion_readdir ($dirP
Ath, $Deep =0) {$resDir =opendir ($dirPath); while ($basename =readdir ($resDir)) {//current file path $path = $dirPath. '
/'. $basename; if (Is_dir ($path) and $basename!= '. ' and $basename!= '.. ') {//is the directory, print directory name, continue iterating echo forchar ('-', $Deep). $basename. '
/<br/> ';
$Deep ++;//Depth +1 recursion_readdir ($path, $Deep); }else if (basename ($path)!= '. ' and basename ($path)!= '.. ') {//Not folder, print filename echo forchar ('-', $Deep). basename ($path). '
<br/> ';
} closedir ($resDir); }
The above PHP recursive traversal of the specified folder in the file implementation method is a small set to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.