[英]get full folders tree and count txt files inside
main folder is home
- with subfolders and txt
files - on various levels主文件夹是
home
文件夹 - 包含子文件夹和txt
文件 - 在各个级别
I need the list of entire folders tree - and count txt
files inside each of them我需要整个文件夹树的列表 - 并计算每个文件夹中的
txt
文件
This code gives the folders but count is always - 0
此代码提供文件夹,但计数始终为 -
0
I suppose paths to folders
and not only folder names - are required, but can't see - how to get them.我想
paths to folders
而不仅仅是文件夹名称 - 是必需的,但看不到 - 如何获取它们。
function rscan ($dir) {
$all = array_diff(scandir($dir), [".", ".."]);
foreach ($all as $ff) {
if(is_dir($dir . $ff)){
echo $ff . "\n"; // it works
$arr = glob($ff . "/*.txt");
echo count($arr) . "\n"; // always 0
rscan("$dir$ff/");
}
}
}
rscan("home/");
Line 6 6号线
$arr = glob($ff . "/*.txt");
Change to the code below:更改为以下代码:
$arr = glob($dir.$ff . "/*.txt");
An alternate implementation:替代实现:
<?php
function glob_recursive($pattern, $flags = 0): Int {
$files = glob($pattern, $flags);
$count = count($files);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$count += glob_recursive($dir.'/'.basename($pattern), $flags);
}
return $count;
}
var_dump(glob_recursive('home/*.txt'));
The output is something like: output 类似于:
int(10)
整数(10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.