Php 列出最后一级的文件夹和子文件夹
我需要监视目录中是否有空文件夹,以便为用户生成警报 由于infra,我想用php编写的这个脚本将每天运行一次 我找到了几个例子,但没有一个能帮助我将空文件夹列到最后一级 例如:Php 列出最后一级的文件夹和子文件夹,php,Php,我需要监视目录中是否有空文件夹,以便为用户生成警报 由于infra,我想用php编写的这个脚本将每天运行一次 我找到了几个例子,但没有一个能帮助我将空文件夹列到最后一级 例如: Root / Folder1 / year / month / file.pdf Root / Pasta2 / year / month 在文件夹2中,我知道它是空的,我需要得到所有这些箱子 我试过了 $di = new RecursiveDirectoryIterator('../Vistorias/'); for
Root / Folder1 / year / month / file.pdf
Root / Pasta2 / year / month
在文件夹2中,我知道它是空的,我需要得到所有这些箱子
我试过了
$di = new RecursiveDirectoryIterator('../Vistorias/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
}
我想要这样的回报:
../Vistorias/Pasta-3/. - 64 bytes
../Vistorias/Paste-2/.. - 256 bytes
../Vistorias/Pasta-1/Sub-pasta-1-a/export.pdf - 3959 bytes
../Vistorias/Pasta-1/vazia/. - 64 bytes
../Vistorias/teste/. - 96 bytes
../Vistorias/teste/trigo.png - 1727287 bytes
所以我知道下面的文件夹是空的:
../Vistorias/Pasta-3/. - 64 bytes
../Vistorias/Paste-2/.. - 256 bytes
有什么想法吗?这样做如何-从文件名中提取路径,将其用作数组的键,并总结您遇到的目录项的数量。对于空目录,该计数将为2-因为条目
和。
。因此,可以使用array_filter从最终数组中删除具有不同计数的任何其他内容
$di = new RecursiveDirectoryIterator('../Vistorias/');
$folders = [];
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$path = pathinfo($filename, PATHINFO_DIRNAME);
// entry for path either already exists, then add one - or initialize with 1
$folders[$path] = isset($folders[$path]) ? $folders[$path] + 1 : 1;
}
// filter all entries with a count != 2, and apply array_keys
// to get the folder names, which are currently the keys, to become the values
// again in the final result
$folders = array_keys(array_filter($folders, function($count) { return $count == 2; }));
到目前为止你试过什么?请看一看,并添加更多的细节到您的问题权利!我为这个问题补充了更多的信息。谢谢你的评论。谢谢你的时间!这正是我所需要的。
$di = new RecursiveDirectoryIterator('../Vistorias/');
$folders = [];
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$path = pathinfo($filename, PATHINFO_DIRNAME);
// entry for path either already exists, then add one - or initialize with 1
$folders[$path] = isset($folders[$path]) ? $folders[$path] + 1 : 1;
}
// filter all entries with a count != 2, and apply array_keys
// to get the folder names, which are currently the keys, to become the values
// again in the final result
$folders = array_keys(array_filter($folders, function($count) { return $count == 2; }));