List all folders in directory (PHP)

Unknown picture Unknown · Dec 27, 2017 · Viewed 7.2k times · Source

How can I make my code only display links to the folders and not the files in the directory?

$d = dir(".");
echo "<ul>";
while(false !== ($entry = $d->read())) {
    echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();

Answer

Tommaso Belluzzo picture Tommaso Belluzzo · Dec 27, 2017
$d = dir(".");

echo "<ul>";

while (false !== ($entry = $d->read()))
{
    if (is_dir($entry) && ($entry != '.') && ($entry != '..'))
        echo "<li><a href='{$entry}'>{$entry}</a></li>";
}

echo "</ul>";

$d->close();