List all folders in directory (PHP) [duplicate]

半世苍凉 提交于 2020-04-16 05:24:29

问题


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();

回答1:


$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();



回答2:


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, ['.', '..'])) {
...



回答3:


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();


来源:https://stackoverflow.com/questions/47998817/list-all-folders-in-directory-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!