PHP计数目录和子目录函数中的文件总数
我需要获得指定目录中JPG文件的总数,包括它的所有子目录。没有子目录 结构如下所示: dir1/ 2 files subdir 1/ 8 filesPHP计数目录和子目录函数中的文件总数,php,file,count,directory,Php,File,Count,Directory,我需要获得指定目录中JPG文件的总数,包括它的所有子目录。没有子目录 结构如下所示: dir1/ 2 files subdir 1/ 8 files A for each循环可以更快地完成此任务;-) 我记得,opendir是从SplFileObject类派生出来的,它是一个递归迭代器、可遍历迭代器、SeekableIterator类,因此,如果使用SPL标准PHP库检索整个图像计数,即使在子目录上也不需要while循环 但是,我已经有一段时间没有使用PHP了,所以我可能
A for each循环可以更快地完成此任务;-) 我记得,opendir是从SplFileObject类派生出来的,它是一个递归迭代器、可遍历迭代器、SeekableIterator类,因此,如果使用SPL标准PHP库检索整个图像计数,即使在子目录上也不需要while循环
但是,我已经有一段时间没有使用PHP了,所以我可能犯了一个错误。为了好玩,我把它拼凑起来:
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()
函数中给定的路径中获得相对路径名
希望它有意义并对您有所帮助:)您可以使用
错误报告(E\u ALL);
功能打印选项卡($level)
{
回声“
”;
$l=0;
对于(;$l<$level;$l++)
“回声”;
}
函数printFileCount($dirName,$init)
{
$fileCount=0;
$st=strrpos($dirName,“/”);
打印标签($init);
echo substr($dirName,$st);
$dHandle=opendir($dirName);
while(false!=($subEntity=readdir($dHandle)))
{
如果($subEntity==“| |$subEntity==”))
继续;
if(是_文件($dirName./'.$subEntity))
{
$fileCount++;
}
else//if(is_dir($dirName./'.$subEntity))
{
printFileCount($dirName.'/'.$subEntity,$init+1);
}
}
打印标签($init);
echo($fileCount.“files”);
返回;
}
printFileCount(“/var/www”,0);
刚刚检查过,正在工作。但是结果的一致性很差,如果有人想要计算文件和目录的总数,那么逻辑是有效的
显示/计数总目录和子目录计数
find . -type d -print | wc -l
显示/计数主目录和子目录中的文件总数
find . -type f -print | wc -l
仅显示/计数当前目录中的文件(无子目录)
显示/计数当前目录中的目录和文件总数(无子目录)
开发人员的答案实际上非常棒!
像这样使用它使其工作:
系统(“查找-类型f-打印| wc-l”) @Neoweiter这也会扫描子目录,我以为你特别想要的是子目录级别?@jack,我不会使用子目录,但如果脚本查找它,就没那么重要了;)您可以使用recursiveiterator
上的方法来限制递归的深度:这里您可能希望将其设置为1
(即,一个子目录深度)。@Lawrence Cherone正是我需要的,谢谢。数组中再多一个文件夹数将使其为100对100:)它包括隐藏的快捷链接,如双点..和点.向下投票,因为这并不能回答问题
<?php
function scan_dir($path){
$ite=new RecursiveDirectoryIterator($path);
$bytestotal=0;
$nbfiles=0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
$filesize=$cur->getSize();
$bytestotal+=$filesize;
$nbfiles++;
$files[] = $filename;
}
$bytestotal=number_format($bytestotal);
return array('total_files'=>$nbfiles,'total_size'=>$bytestotal,'files'=>$files);
}
$files = scan_dir('./');
echo "Total: {$files['total_files']} files, {$files['total_size']} bytes\n";
//Total: 1195 files, 357,374,878 bytes
?>
error_reporting(E_ALL);
function printTabs($level)
{
echo "<br/><br/>";
$l = 0;
for (; $l < $level; $l++)
echo ".";
}
function printFileCount($dirName, $init)
{
$fileCount = 0;
$st = strrpos($dirName, "/");
printTabs($init);
echo substr($dirName, $st);
$dHandle = opendir($dirName);
while (false !== ($subEntity = readdir($dHandle)))
{
if ($subEntity == "." || $subEntity == "..")
continue;
if (is_file($dirName . '/' . $subEntity))
{
$fileCount++;
}
else //if(is_dir($dirName.'/'.$subEntity))
{
printFileCount($dirName . '/' . $subEntity, $init + 1);
}
}
printTabs($init);
echo($fileCount . " files");
return;
}
printFileCount("/var/www", 0);
find . -type d -print | wc -l
find . -type f -print | wc -l
find . -maxdepth 1 -type f -print | wc -l
ls -1 | wc -l