PHP 代码
<?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>';
?>
问题
我希望能够计算我所选位置的文件夹数量,以便使用此脚本执行更多操作,该脚本对子文件夹的数量执行 while 循环。
count($dir)
将是最简单的解决方案,但不幸的是它在这里不起作用。(始终为 1)
所以这是带有计数器变量的解决方案:
<?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.";
?>