Reputation: 360
<?php
echo '<ul class="DirView">';
$path = "../Desktop/";
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo '<li>'.$fileinfo->getFilename().'</li>';
}
}
echo '</ul>';
?>
I wish to be able to count the number of folders in my chosen location to do more with this script performing a whilst loop for the amount of sub-folders there are.
Upvotes: 1
Views: 1988
Reputation: 66
count($dir)
would be the easiest solution, but unfortunately it doesn't work here. (always 1)
So here is the solution with a counter variable:
<?php
echo '<ul class="DirView">';
$path = "..";
$dir = new DirectoryIterator($path);
$counter = 0;
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo '<li>'.$fileinfo->getFilename().'</li>';
$counter++;
// do your while loop here
}
}
echo '</ul>';
echo "There are $counter files in this directory.";
?>
Upvotes: 3