PHP计数目录和子目录中的总文件功能

2021-12-30 00:00:00 directory file count php

我需要获取指定目录中 JPG 文件的总数,包括它的所有子目录.没有子目录.

结构如下:

<前>目录 1/2个文件子目录 1/8个文件

总共 dir1 = 10 个文件

<前>目录2/5个文件子目录 1/2个文件子目录 2/8个文件

总共 dir2 = 15 个文件

我有这个功能,它不能正常工作,因为它只计算最后一个子目录中的文件,总数是实际文件数量的 2 倍.(如果我在最后一个子目录中有 40 个文件,将输出 80)

公共函数count_files($path) {全局 $file_count;$file_count = 0;$dir = opendir($path);如果 (!$dir) 返回 -1;而 ($file = readdir($dir)) :if ($file == '.' || $file == '..') continue;如果 (is_dir($path . $file)) :$file_count += $this->count_files($path . "/" . $file);别的 :$file_count++;万一;终了;关闭 ($dir);返回 $file_count;}

解决方案

为了好玩,我把它放在一起:

class FileFinder{私人 $onFound;私有函数 __construct($path, $onFound, $maxDepth){//onFound 在每个找到的文件时被调用$this->onFound = $onFound;//立即开始迭代$this->iterate($path, $maxDepth);}私有函数迭代($path,$maxDepth){$d = opendir($path);而 ($e = readdir($d)) {//跳过特殊文件夹if ($e == '.' || $e == '..') { 继续;}$absPath = "$path/$e";如果(is_dir($absPath)){//在进入下一次递归之前先检查 $maxDepth如果($maxDepth != 0){//减少下一次迭代的最大深度$this->iterate($absPath, $maxDepth - 1);}} 别的 {//找到常规文件,调用找到的处理程序call_user_func_array($this->onFound, array($absPath));}}关闭 ($d);}//实例化一个 finder 对象的辅助函数//虽然返回值不是很重要,因为所有方法都是私有的公共静态函数 find($path, $onFound, $maxDepth = 0){return new self($path, $onFound, $maxDepth);}}//开始查找文件(最大深度为向下一个文件夹)$count = $bytes = 0;FileFinder::find('.', function($file) use (&$count, &$bytes) {//闭包更新到目前为止的计数和字节数++$计数;$bytes += 文件大小($file);}, 1);echo "Nr 个文件:$count;使用的字节数:$bytes
";

您传递基本路径、找到的处理程序和最大目录深度(-1 表示禁用).找到的处理程序是您在外部定义的一个函数,它从 find() 函数中给出的路径中获取相对路径名.

希望它有意义并能帮助你:)

I need to get a total count of JPG files within a specified directory, including ALL it's subdirectories. No sub-sub directories.

Structure looks like this :

dir1/
2 files  
   subdir 1/
       8 files

total dir1 = 10 files

dir2/ 
    5 files  
    subdir 1/ 
        2 files  
    subdir 2/ 
        8 files

total dir2 = 15 files

I have this function, which doesn't work fine as it only counts files in the last subdirectory, and total is 2x more than the actual amount of files. (will output 80 if I have 40 files in the last subdir)

public function count_files($path) { 
global $file_count;

$file_count = 0;
$dir = opendir($path);

if (!$dir) return -1;
while ($file = readdir($dir)) :
    if ($file == '.' || $file == '..') continue;
    if (is_dir($path . $file)) :
        $file_count += $this->count_files($path . "/" . $file);
    else :
        $file_count++;
    endif;
endwhile;

closedir($dir);
return $file_count;
}

解决方案

For the fun of it I've whipped this together:

class FileFinder
{
    private $onFound;

    private function __construct($path, $onFound, $maxDepth)
    {
        // onFound gets called at every file found
        $this->onFound = $onFound;
        // start iterating immediately
        $this->iterate($path, $maxDepth);
    }

    private function iterate($path, $maxDepth)
    {
        $d = opendir($path);
        while ($e = readdir($d)) {
            // skip the special folders
            if ($e == '.' || $e == '..') { continue; }
            $absPath = "$path/$e";
            if (is_dir($absPath)) {
                // check $maxDepth first before entering next recursion
                if ($maxDepth != 0) {
                    // reduce maximum depth for next iteration
                    $this->iterate($absPath, $maxDepth - 1);
                }
            } else {
                // regular file found, call the found handler
                call_user_func_array($this->onFound, array($absPath));
            }
        }
        closedir($d);
    }

    // helper function to instantiate one finder object
    // return value is not very important though, because all methods are private
    public static function find($path, $onFound, $maxDepth = 0)
    {
        return new self($path, $onFound, $maxDepth);
    }
}

// start finding files (maximum depth is one folder down) 
$count = $bytes = 0;
FileFinder::find('.', function($file) use (&$count, &$bytes) {
    // the closure updates count and bytes so far
    ++$count;
    $bytes += filesize($file);
}, 1);

echo "Nr files: $count; bytes used: $bytes
";

You pass the base path, found handler and maximum directory depth (-1 to disable). The found handler is a function you define outside, it gets passed the path name relative from the path given in the find() function.

Hope it makes sense and helps you :)

相关文章