Cette page explique comment récupérer la liste des sous-répertoires d'un répertoire (racine) donné en PHP.
La fonction suivante retourne la liste complète des sous-répertoires de $dir
:
// Return an array with the list of sub directories of $dir
function getSubDirectories($dir)
{
$subDir = array();
// Get and add directories of $dir
$directories = array_filter(glob($dir), 'is_dir');
$subDir = array_merge($subDir, $directories);
// Foreach directory, recursively get and add sub directories
foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory.'/*'));
// Return list of sub directories
return $subDir;
}
Version minimale :
function getSubDirectories($dir)
{
$subDir = array();
$directories = array_filter(glob($dir), 'is_dir');
$subDir = array_merge($subDir, $directories);
foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory.'/*'));
return $subDir;
}