php 计算目录下的文件数,PHP计算目录AND子目录函数中的总文件数

为了它的乐趣,我把它鞭打在一起:

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()函数中给出的路径相对传递路径名.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值