PHP中,什么函数可以统计一个目录中共有多少个文件

来源:百度知道 编辑:UC知道 时间:2024/06/05 10:40:46
例如有一个目录,名字叫"dir"
里面有若干个不同类型的文件,例如php,asp,txt之类的

1)怎样统计该目录下一共有多少个文件
2)怎样统计该目录下一共有多少个php文件
3)怎样统计该目录下一共有多少个非txt文件
$arr = scandir($dir);
$all = count($arr)-2;//所有文件总数除./和../
这个答案是正确,可以算出实际拥有的文件数.
不过,经过测试,会连文件夹也算进去,
我只需要计算共有多少个文件,而不需要计算文件夹.

$arr = scandir($dir);
$all = count($arr)-2;//所有文件总数除./和../
$php = count(preg_grep("/\.php$/", $arr));
$txt0 = $all - count(preg_grep("/\.txt$/", $arr));
echo '共有'.$all.'个文件,php文件'.$php.'个,非txt文件'.$txt0.'个';
希采纳

$arr = scandir($dir);
$all = count($arr)-2;//所有文件总数除./和../
$php = count(preg_grep("/\.php$/", $arr));
$txt0 = $all - count(preg_grep("/\.txt$/", $arr));
echo '共有'.$all.'个文件,php文件'.$php.'个,非txt文件'.$txt0.'个';

这样,比如你的dir在c盘
<?php
$dirName = "C:\dir";
$dp = opendir($dirName);

while($currentFile !== false){
$currentFile = readDir($dp);
$theFiles[] = $currentFile;
}

$allFiles = count($theFiles);//1、所有文件总数
$phpFiles = count(preg_grep("/php$/", $theFiles));//2、php文件个数
$nontxtFiles = $allFiles - count(preg_grep("/t