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 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)