We often encounter this problem in the interview: PHP Traverse all the files and subfolders under a folder.
There are many solutions to this problem. But the general idea is the same. Recursive.
$ handle = opendir ($ path); while (false! == ($ handle = $ ./filepath); function getfiles ($ path) {if (! is_dir ($ path)) return; if (is_dir ($ path2)) {echo ($ path! = '.' & amp; $ file! = '..') {$ path2 = $ path. '/' echo $ file; getfiles ($ path2);} else {echo '' echo $ file;}}} print_r (getfiles ($ path)); echo '<HR>'; function getdir ($ path) {if (! is_dir ($ path)) return; $ handle = dir ($ path); while ($ file = $ handle- & gt; read ()) {if ($ file! = '.' & amp; if (is_dir ($ path2)) {echo $ file. "t"; getdir ($ path2);} else {echo $ file. ' ($ path) as $ single) {if ($ single! ';}}}} getdir ($ path); echo' <HR> 'function get_dir_scandir ($ path) ($ path2 = $ path. '/'. $ single; if (is_dir ($ path2)) {echo $ single. "rn"; get_dir_scandir ( $ path = array ();} else {echo $ single. "rn";}}} get_dir_scandir ($ path); echo '<HR>'; function get_dir_glob / curl / * ') as $ single) {echo $ single. "rn";}} get_dir_glob (); echo' <HR> '; function myscandir ($ path) {if (! is_dir ($ path)) return; foreach (scandir ($ path) as $ file) {if ($ file! = '.' & amp; $ file! = '..') {$ path2 = $ path. '/'. $ file; if ( is_dir ($ path2)) {echo }}}} myscandir ($ path); echo '<HR>'; function myglob ($ path) {$ path_pattern = $ path. ' / * '; foreach (glob ($ path_pattern) as $ file) {if (is_dir ($ file)) {echo $ file; myscandir ($ file);} else {echo $ file.' ';}}} myglob ( $ path);