问题
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