2

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();
GrumpyCrouton
  • 8,171
  • 5
  • 29
  • 65
Unknown
  • 39
  • 1
  • 6

3 Answers3

8
$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();
Tommaso Belluzzo
  • 22,356
  • 7
  • 68
  • 95
3

You should just be able to wrap your current code with a call to is_dir:

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

If you want to remove the "dot" directories (. and ..), use the following:

if (is_dir($entry) && !in_array($entry, ['.', '..'])) {
...
iainn
  • 16,011
  • 9
  • 29
  • 38
1

Just check if the $entry is a directory:

$d = dir(".");
echo "<ul>";
while(false !== ($entry = $d->read())) {
if(is_dir($entry))
    echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();
RDev
  • 1,126
  • 1
  • 10
  • 17