Use PHP to recursively loop through each directory

  • 2020-03-31 21:00:14
  • OfStack

The principle of the function is simple, mainly with a recursive call.
 
function file_list($path){ 
if ($handle = opendir($path)) { 
while (false !== ($file = readdir($handle))) { 
if ($file != "." && $file != "..") { 
if (is_dir($path."/".$file)) { 
echo $path.": ".$file."<br>";//Remove this line to show all non-directory files
file_list($path."/".$file); 
} else { 
echo $path.": ".$file."<br>"; 
} 
} 
} 
} 
} 

This function can also continue to do some improvements, add some folders or file ICONS, so you can make a more powerful function, interested friends can be extended.

Related articles: