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

2023-07-16php开发问题
2

本文介绍了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 :)

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17