Here's a list of the directories in the current folder (they are all directories, there are no files or links):
Code:
./temp
./temp/sub1
./temp/sub1/lowest1
./temp/sub1/lowest2
./temp/sub2
./temp/sub2/lowest3
./temp/sub2/lowest4
Here's the PHP script in the current folder:
PHP Code:
<?php
    
//Make the iterator for the temp directory
    
$dir = new DirectoryIterator('temp');

    
//Print the top level name
    
echo $dir->getBasename(), '<br>';

    
//Loop through all the subdirectories
    
foreach ($dir as $subdir) {
        if (!
$subdir->isDir() || $subdir->isDot()) continue;

        
//Print the subdirectory name
        
echo '> '$subdir->getBaseName(), '<br>';

        
//Loop through all the (sub)subdirectories
        
foreach ($subdir as $subsubdir) {
            if (!
$subsubdir->isDir() || $subsubdir->isDot()) continue;

            
//Print the (sub)subdirectory name
            
echo '>> '$subsubdir->getBaseName(), '<br>';
        }
    }
?>
I would expect this output:
Code:
.
> sub1
>> lowest1
>> lowest2
> sub2
>> lowest3
>> lowest4
However, the code produces this:
Code:
.
> sub1
>> sub1
>> sub2
In fact, it doesn't matter how many loops I nest; it stays in the same directory.

Adding this line before the inner loop fixes the issue:
PHP Code:
$subdir = new DirectoryIterator($subdir->getRealPath()); 
Where did I go wrong?

For my purposes, I need to recurse to a depth of exactly 2; hence I am not using RecursiveDirectoryIterator.