简体   繁体   中英

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.....

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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