Here's a list of the directories in the current folder (they are all directories, there are no files or links):
Here's the PHP script in the current folder:Code:./temp ./temp/sub1 ./temp/sub1/lowest1 ./temp/sub1/lowest2 ./temp/sub2 ./temp/sub2/lowest3 ./temp/sub2/lowest4
I would expect this output: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>';
}
}
?>
However, the code produces this:Code:. > sub1 >> lowest1 >> lowest2 > sub2 >> lowest3 >> lowest4
In fact, it doesn't matter how many loops I nest; it stays in the same directory.Code:. > sub1 >> sub1 >> sub2
Adding this line before the inner loop fixes the issue:
Where did I go wrong?PHP Code:$subdir = new DirectoryIterator($subdir->getRealPath());
For my purposes, I need to recurse to a depth of exactly 2; hence I am not using RecursiveDirectoryIterator.











Bookmarks