drlndkhib08556095 2011-08-10 19:14
浏览 64
已采纳

php找到文件夹的名称,然后找到那里的文件?

I am trying to use php to look in a folder and there will be five folders within that ....i want to display the name as the folder as the ul and the names of that folders files as li's.....

here is my code I i creaded to create the inner li's but hos do i find the folder names

$counter = 0;
$location = "main_folder";
$main_directory = $_SERVER['DOCUMENT_ROOT'] . "/sandbox/{$location}/";

$dir = opendir ($directory);
        while (false !== ($file = readdir($dir))) {
            if ($file != "." && $file != "..") {
            $id = str_replace('.png', '', $file);
            $display_name = str_replace('-', ' ', $id);                             
            echo "<li data='{$id}'>{$display_name}</li>"
            $counter++;
            }       
     }

the folder structure is like this

main_folder/inner1/Dog-Park.png Cat-Store.png .....
main_folder/inner2/...
main_folder/inner3/...
main_folder/inner4/...
main_folder/inner5/...

here is what i want the final outcome to be

<ul style='display:none;' rel='inner1' class='left_ul'>
  <li data='Dog-Park'>Dog Park</li>
  <li data='Cat-Store'>Cat Store</li>
  <li data='Add-Gratuity'>Add Gratuity</li>
...
...
...
</ul>

any ideas about how to do the folders

  • 写回答

1条回答 默认 最新

  • doukengzi3517 2011-08-10 20:23
    关注

    You should have an opendir() inside the opendir() (excuse my formatting):

    $dir = opendir ($directory);
            while (false !== ($file = readdir($dir))) {
                if ($file != "." && $file != ".." && file_type($file) == 'dir') {
    echo '<ul>';
    $dir2 = opendir ($directory.'/'.$file);
            while (false !== ($file2 = readdir($dir2))) {
                if ($file2 != "." && $file2 != ".." && file_type($file2) == 'file') {
                $id = str_replace('.png', '', $file);
                $display_name = str_replace('-', ' ', $id);                             
                echo "<li data='{$id}'>{$display_name}</li>"
    }}
    closedir($directory.'/'.$file);
    Echo '</ul>';
                }       
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计