列出文件夹和子文件夹,但不列出文件

时间:2018-05-01 12:52:30

标签: php

我研究过但没有找到答案,你知道如何查找所有文件夹和子文件夹,但不是文件吗?

<ul>
   <li><a href="/Folder1">Folder 1</a></li>
       <ul>
           <li><a href="/Folder1/Subfolder1">Subfolder 1</a></li>
           <li><a href="/Folder1/Subfolder2">Subfolder 2</a></li>
           <li><a href="/Folder1/Subfolder3">Subfolder 3</a></li>
       </ul>
   <li><a href="/Folder2">Folder 2</a></li>
       <ul>
           <li><a href="/Folder2/Subfolder1">Subfolder 1</a></li>
           <li><a href="/Folder2/Subfolder2">Subfolder 2</a></li>
       </ul>
   <li><a href="/Folder3">Folder 3</a></li>
</ul>

我在stackoverflow上找到了这个代码,但它显示了文件,我不知道如何为a href做路径文件夹:

function listFolderFiles($dir){
$ffs = scandir($dir);

unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);

// prevent empty ordered elements
if (count($ffs) < 1)
    return;

echo '<ol>';
foreach($ffs as $ff){
    echo '<li>'.$ff;
    if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);

    echo '</li>';
}
echo '</ol>';
}
listFolderFiles('/Applications/MAMP/htdocs/Test');

1 个答案:

答案 0 :(得分:1)

只检查文件是否为目录:

foreach($ffs as $ff){

  if (!is_dir($ff)) {
     continue;
  }

  echo '<li>'.$ff;
  if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
  echo '</li>';
}