PHP计数总文件在目录AND子目录中的功能 [英] PHP count total files in directory AND subdirectory function

查看:135
本文介绍了PHP计数总文件在目录AND子目录中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

结构如下:


dir1/
2 files  
   subdir 1/
       8 files

dir1 = 10个文件


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

总计 dir2 = 15个档案

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

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;
}


推荐答案

'vepped this together:

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\n";

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

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 :)

这篇关于PHP计数总文件在目录AND子目录中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆