Home >Backend Development >PHP Tutorial >Two functions for php to recursively traverse directories
The two functions for recursively traversing directories implemented in PHP use functions such as glob, is_dir, and dir in PHP. Friends in need can refer to them.
Achieved with the help of PHP built-in functions glob, is_dir, and dir. <?php /** * 函数1 * 递归遍历目录 * site bbs.it-home.org */ function myscandir($pathname){ foreach( glob($pathname) as $filename ){ if(is_dir($filename)){ myscandir($filename.'/*'); }else{ echo $filename.'<br/>'; } } } myscandir('D:/wamp/www/exe1/*'); /** * 函数2 * 使用dir()函数 */ function myscandir($path){ $mydir=dir($path); while($file=$mydir->read()){ $p=$path.'/'.$file; if(($file!=".") AND ($file!="..")){ echo $p.'<br>'; } if((is_dir($p)) AND ($file!=".") AND ($file!="..")){ myscandir($p); } } } myscandir(dirname(dirname(__FILE__))); ?> |