1

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

4

1 に答える 1

1

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>';
            }       
     }
于 2011-08-10T20:23:09.393 に答える