Don't show images when no subfolder available - php readdir()

I took the following script and adapted to my needs but have one problem. I don’t want the files to show in that directory. I only want to see the folders. I’m using this for a menu that is folder driven so it’s easy for my client to just upload a folder of images and the menu updates. Any help.


$dir = opendir('images/gallery_indiv/');
echo '<ul class="topnav">';

while ($read = readdir($dir))
{
if ($read!='.' && $read!='..')
{
echo '<li><a href="#">'.$read.'</a>';
$dir2 = opendir('images/gallery_indiv/'.$read.'');
echo '<ul>';

while ($read2 = readdir($dir2))
{

if ($read2!='.' && $read2!='..')
{
echo '<li><a href="gallery3.php?room='.$read.'&folder='.$read2.'">'.$read2.'</a></li></li>';
}

}

echo '</ul>';

closedir($dir2);
}

}

echo '</ul>';

closedir($dir); 

This outputs:
Folder - Subfolder
Folder - Images *when no subfolder is available
Folder -Subfolder
Folder - Images *when no subfolder is available

I would like not in this order but depending on the folder structure as exampleDon’t:
Folder - Subfolder
Folder - *No subfolder, no images displayed
Folder - Subfolder
Folder - Subfolder
Folder - *No subfolder, no images displayed

Use is_dir to check if $read and/or $read2 is a directory.

$dir = opendir('images/gallery_indiv/');
echo '<ul class="topnav">';

while ($read = readdir($dir))
{
if ($read!='.' && $read!='..')
{
echo '<li><a href="#">'.$read.'</a>';
$dir2 = opendir('images/gallery_indiv/'.$read.'');
echo '<ul>';

while ($read2 = readdir($dir2))
{
if(is_dir($read2)){
if ($read2!='.' && $read2!='..')
{
echo '<li><a href="gallery3.php?room='.$read.'&folder='.$read2.'">'.$read2.'</a></li></li>';
}
}
}

echo '</ul>';

closedir($dir2);
}

}

echo '</ul>';

closedir($dir); 

ok, so I tried the above suggestion and I can’t get ANY directory to show after the first layer. I have tried to set the $dir2 to the absolute path and still nothing. Any more suggestions or what am I missing?
Safe Mode = Off
open_basedir = no value

ok so after more playing around I’m more confused.
is_dir() doesn’t see any folders except “.” and “…” but when I make them not available I can’t see any folders.
What am I doing wrong that it won’t see any folders in the directory tree?

Something like the following will do the job

$baseDir = "./images/gallery_indiv/";
$dir = @opendir($baseDir);
if ($dir !== false)
{
        echo "<ul class=\\"topnav\\">\
";
        while ($read = @readdir($dir))
        {
                if (is_dir("$baseDir/$read") && $read != '.' && $read != '..')
                {
                        echo "<li><a href=\\"#\\">$read</a>\
";
                        $dir2 = @opendir("$baseDir/$read");
                        if ($dir2 !== false)
                        {
                                echo '<ul>';
                                while ($read2 = readdir($dir2))
                                {
                                        if (is_dir("$baseDir/$read/$read2") && $read2 != '.' && $read2 != '..')
                                                echo '<li><a href="gallery3.php?room='.$read.'&folder='.$read2.'">'.$read2.'</a></li></li>' . "\
";
                                }

                                echo "</ul>\
";

                                closedir($dir2);
                        }
                        echo "</li>\
";
                }
        }
        echo "</ul>\
";

        closedir($dir);
}

ps. From php’s manual: “filename: Path to the file. If filename is a relative filename, it will be checked relative to the current working directory. If filename is a symbolic or hard link then the link will be resolved and checked.”

That worked perfect except a small { issue towards the bottom to close up the first if statement. Thank you very much.

I do however have one question about making this work even better. it’s probably not possible with the setup but is there anyways that if the folder doesn’t have a subfolder inside that it doesn’t insert the <ul> right off the bat?

The problem I am having, not really a problem but it’s kind of annoying with the menu is because there is a . and … in the folder the dir2 returns true everytime till it gets to the statement about excluding the . and …
I tried just moving the <ul> inside that statement but it just messes everything up.

example from the source code.

<ul>
<li><a href="#">Living Room</a>
<ul></ul>  //Here is an empty tag and it makes the menu think that it has to add a plus sign because it's there.
The folder only has images in it, not another directory
</li>
<li><a href="#">Studies</a>
<ul>
<li><a href="#">Red</a></li>
<li><a href="#">Wooden</a></li>
</ul>

Other than that small annoyance this script was perfect and I am very grateful for your assistance! Thank You again!

$baseDir = "./images/gallery_indiv";
$dir = @opendir($baseDir);
if ($dir !== false)
{
        echo "<ul class=\\"topnav\\">\
";
        while ($read = @readdir($dir))
        {
                if (is_dir("$baseDir/$read") && $read != '.' && $read != '..')
                {
                        echo "<li><a href=\\"#\\">$read</a>\
";
                        $dir2 = @opendir("$baseDir/$read");
                        if ($dir2 !== false)
                        {
                                $thisDirData = array();
                                while ($read2 = readdir($dir2))
                                {
                                        if (is_dir("$baseDir/$read/$read2") && $read2 != '.' && $read2 != '..')
                                                $thisDirData[] = '<li><a href="gallery3.php?room='.$read.'&folder='.$read2.'">'.$read2.'</a></li>';
                                }

                                if (isset($thisDirData[0]))
                                {
                                        echo "<ul>" . implode("\
", $thisDirData) . "</ul>\
";
                                }
                                closedir($dir2);
                        }
                        echo "</li>\
";
                }
        }
        echo "</ul>\
";

        closedir($dir);
}

Just wanted to let you know how grateful I am for the help you have provided. It worked perfectly! Thank You Very Much!