duan1982453 2012-10-20 00:34
浏览 18
已采纳

列出菜单系统中的所有文件夹

I'm trying to use scandir() to list all folders in a directory into a menu structure I am making. Here is the code I have so far:

$directory = "models/";
$files = scandir($directory);

echo "  <a class=\"menuitem submenuheader\" href=\"#\" >models</a>
        <div class=\"submenu\">

        <ul>
";

foreach($files as $file)
{
     if(is_dir($file))
     {
        echo "<li><a href=\"$file.html\">$file</a></li>
";
     }
}
echo "</ul>
</div>
";

I think I'm having an issue in the if(is_dir($file) line of the code. It is listing two items in the menu (.) and (..).

Thanks for any/all help that is offered! I very much appreciate it.

To do some testing, I replaced the foreach loop with a for loop.

$directory = "models/";
$folders = scandir($directory);

echo "<a class=\"menuitem submenuheader\" href=\"#\" >models</a>
";
echo "<div class=\"submenu\">
";
echo "<ul>
";

for ($i=0; $i<count($folders); $i++) {
     if ($folders[$i] != '.' && $folders[$i] != '..') {
         if(is_dir($folders[$i])) {
            echo "<li><a href=\"" . $folders[$i] . ".html\">" . $folders[$i] . "</a></li>
";
         }
     }
}
echo "</ul>
</div>
";

Still doesn't work (Won't pull a folder within the $directory) BUT, if I change the

if(is_dir($folders[$i]))

to

if(!is_dir($folders[$i]))

it lists all the files as well as the single folder in the $directory. Not sure why it is doing this. Any ideas?

  • 写回答

2条回答 默认 最新

  • 普通网友 2012-10-20 01:26
    关注

    GOT IT!!!

    I guess is_dir() must list the FULL PATH to the folder in question, otherwise, it doesn't work. I changed my code to this:

    if(is_dir($directory . $folders[$i])) {
    

    and it works perfect!

    Thanks again for everyones help! Brainstorming with this site gets my brain juices flowing...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化