计算每个文件夹中的文件

时间:2014-08-11 20:13:17

标签: php

我尝试计算每个子文件夹中的文件。例如

第一个子文件夹:4个文件
第二个子文件夹:6个文件 ...

现在我的函数计算目录中的文件,我在数组中选择:

if (!is_dir($subentry) && $gallery_dir[0]) {

如何计算和列出每个子目录中的文件数?但不是所有子目录中的sum元素,而是每个子目录中的元素从0开始计数。

这是我到目前为止所尝试的:

$folderCount = $fileCount = $galleryItemCount = 0;
$dir ='./product_img/';
//. means current directory
//if you wanna a learn a folders inside use opendir('path')
$gallery_dir = array();
$gallery_subdir = array();
if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            if (is_dir($dir.$entry)) {
                echo "Folder => " . $entry . "<br>";

                    $gallery_dir[] = $entry;

                    if ($subdir = opendir($dir.$entry)) {
                         while (false !== ($subentry = readdir($subdir))) {
                            if ($subentry != "." && $subentry != "..") {
                                if (!is_dir($subentry) && $gallery_dir[0]) {
                                    echo "Folderrrr => " . $subentry . "<br>";  
                                    $gallery_subdir[] = $subentry;
                                    $galleryItemCount++;
                                    echo $gallery_dir;
                                }
                            }
                         }
                        closedir($subdir);
                    }
                 $folderCount++;
            } else {
                echo "File   => " . $entry . "<br>";
                $fileCount++;
            }
        }
    }
    //echo $galleryItemCount++;
    echo "<br><br>";
    echo "File in aech folder" . $galleryItemCount. "<br />";
    echo "Total Folder Count : " . $folderCount . "<br>" ;
    echo "Total File Count : " . $fileCount;
    closedir($handle);
} 

3 个答案:

答案 0 :(得分:2)

$dir = "/some/directory";
$counts = array (
    $dir => 0
);

$iterator = new RecursiveDirectoryIterator ($dir, FilesystemIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator ($iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $pathname => $file)
{
    if ($file->isDir ())
    {
        $counts [$file->getPathname ()] = 0;
        continue;
    }
    $counts [$file->getPath ()] ++;
}

答案 1 :(得分:2)

您可以使用迭代器执行此操作。获取每个文件夹的文件数有点棘手。

首先,连接迭代器。传递您想要遍历的路径:

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator(
        '/path/you/want/to/traverse',
        RecursiveDirectoryIterator::SKIP_DOTS
    ),
    RecursiveIteratorIterator::SELF_FIRST
);

选项SKIP_DOTS会使RecursiveDirectoryIterator忽略...。包含目录需要选项SELF_FIRST。默认情况下,只有叶子,例如文件,被认为。

Iterator接线后,你只需要做

foreach ($iterator as $fileObject) {
    if ($fileObject->isDir()) {
        $fileObject->setInfoClass(FilesystemIterator::class);
        $directoryIterator = $fileObject->getFileInfo();
        $directoryIterator->setFlags(FilesystemIterator::SKIP_DOTS);
        $fileCount = iterator_count($directoryIterator);

        echo "$fileObject => $fileCount", PHP_EOL;
    }
}

这将打印每个文件夹名称以及每个文件夹中的文件计数。就像我说的那样,获取文件计数有点棘手。这是因为默认情况下foreach将返回目录的SplFileObject个实例。那些是不可数的。因此,我们需要设置FileSystemIterator。这可以计算在内。

注意:如果您的PHP版本尚未支持FilesystemIterator::class,请更新为更新版本或将'FileSystemIterator'作为字符串传递。

答案 2 :(得分:0)

示例功能:

    function dirs($dir)
    {
        if ($handle = opendir($dir)) {
            while ($DF = readdir($handle)) {
                if ($DF != '.' && $DF != '..') {
                    $TYPE = is_dir($dir . "/" . $DF)?":DIRS":":FILES";
                    $List[$dir][$TYPE] = isset($List[$dir][$TYPE]) ? ++$List[$dir][$TYPE] : 1;
                    $List[$dir . "/" . $DF] = is_dir($dir . "/" . $DF) ? dirs($dir . "/" . $DF) : $DF;
                }
            }
        }
        return $List;
    }

    var_dump(dirs("framework"));

将返回文件和文件夹的数组列表,其中包含DIRS / FILES的COUNT索引

            array (size=9)
      'framework' => 
        array (size=2)
          ':FILES' => int 6
          ':DIRS' => int 2
      'framework/.htaccess' => string '.htaccess' (length=9)
      'framework/composer.json' => string 'composer.json' (length=13)
      'framework/config.ini' => string 'config.ini' (length=10)
      'framework/index.php' => string 'index.php' (length=9)
      'framework/lib' => 
        array (size=24)
          'framework/lib' => 
            array (size=2)
              ':DIRS' => int 3
              ':FILES' => int 20