简体   繁体   English

php 找到文件夹的名称,然后找到其中的文件?

[英]php finding the names of the folders and then the files in there?

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.....我正在尝试使用 php 来查看一个文件夹,其中将有五个文件夹....

here is my code I i creaded to create the inner li's but hos do i find the folder names这是我创建的用于创建内部 li 的代码,但是我是否找到了文件夹名称

$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关于如何做文件夹的任何想法

You should have an opendir() inside the opendir() (excuse my formatting):您应该在 opendir() 中有一个 opendir() (请原谅我的格式):

$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>';
            }       
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM