Core PHP

List Subdirectories

We often have the need to find all of the subdirectories of a particular directory. The code below demonstrates how to list the subdirectories of the current directory.

ListSubdirectories.php

<?php
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "</head>";
echo "<body>";
echo "<ul>";
$sDirectories = glob('*' , GLOB_ONLYDIR | GLOB_NOSORT);
foreach($sDirectories as $sCurrDir) {
    $sDirPath = dirname(__FILE__)."/".$sCurrDir;
    $qDirPathInfo = PathInfo($sDirPath);
    // Make the first letter uppercase, if it is not already.
    echo "<li>".ucfirst($qDirPathInfo['basename'])."</li>";
}
echo "</ul>";
echo "</body>";
echo "</html>";
?>
 

Output

  • Bin
  • Obj
  • Program Files
  • Temp
  • Windows
 

Directory

Directories
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.