This article mainly introduces PHP commonly used three kinds of traversal tree skills, interested in the friend's reference, I hope to be helpful to everyone.
This article describes the common methods of the PHP traversal tree, as follows:
First, recursive depth-first algorithm:
<?phpdefine (' DS ', directory_separator), function rec_list_files ($from = '. ') {if (!is_dir ($from)) {return array (); } $files = Array (); if ($dh = Opendir ($from)) {while (false!== ($file = Readdir ($DH))) {if ($file = = '. ' | | $file = = ' ... ') {continue; } $path = $from. Ds. $file; if (Is_file ($path)) {$files [] = $path; } $files = Array_merge ($files, Rec_list_files ($path)); } closedir ($DH); } return $files;} function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo ' <pre>-----------------------Test run for '. $func. ' () '; Flush (); $time _start = Microtime (true); $list = $func ($trydir); Print_r ($list); $time = Microtime (True)-$time _start; Echo ' finished: '. Count ($list). ' Files</pre> '; $mem 2 = Memory_get_peak_usage (); printf (' <pre>max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. ' ():%0.f s</pre> ', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' ReC_list_files ', "D:\www\server");? >
Second, recursive depth-first algorithm (implemented with a stack)
<?phpdefine (' DS ', directory_separator), function deep_first_list_files ($from = '. ') {if (!is_dir ($from)) {return false; } $files = Array (); $dirs = Array ($from); while (NULL!== ($dir = Array_pop ($dirs))) {if ($dh = Opendir ($dir)) {while (false!== ($file = Readdir ($DH))) { if ($file = = '. ' | | $file = = ' ... ') {continue; } $path = $dir. Ds. $file; if (Is_dir ($path)) {$dirs [] = $path; } else {$files [] = $path; }} closedir ($DH); }} return $files;} function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo ' <pre>-----------------------Test run for '. $func. ' () '; Flush (); $time _start = Microtime (true); $list = $func ($trydir); Print_r ($list); $time = Microtime (True)-$time _start; Echo ' finished: '. Count ($list). ' Files</pre> '; $mem 2 = Memory_get_peak_usage (); printf (' <pre>max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. ' ():%0.F S</pre> ', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' deep_first_list_files ', ' D:\www\server ');? >
Three, a non-recursive breadth-first algorithm (implemented with a queue)
<?phpdefine (' DS ', directory_separator), function breadth_first_files ($from = '. ') {$queue = Array (RTrim ($from, DS). DS);//normalize all paths $files = Array (); while ($base = Array_shift ($queue)) {if ($handle = Opendir ($base))} {while (($child = Readdir ($handle))!== FAL SE) {if ($child = = '. ' | | $child = = ' ... ') {continue; } if (Is_dir ($base. $child)) {$combined _path = $base. $child. DS; Array_push ($queue, $combined _path); } else {$files [] = $base. $child; }} closedir ($handle); }//Else unable to open directory = NEXT child} return $files; End of tree, file not found}function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo ' <pre>-----------------------Test run for '. $func. ' () '; Flush (); $time _start = Microtime (true); $list = $func ($trydir); Print_r ($list); $time = Microtime (True)-$time _start; Echo ' finished: '. Count ($list). ' Files</pre> '; $mem 2 = Memory_get_peak_usage (); printf (' <pre>max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. ' ():%0.f s</pre> ', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' breadth_first_files ', ' D:\www\server ');? >
Summary: The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
PHP implementation of integrated Discuz user approach
PHP Processing session Function Summary sharing
PHP four basic sorting algorithms and two search algorithms