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

例如有一个目录,名字叫"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.'个';
希采纳
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-27
$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.'个';
第2个回答  2009-02-09
$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.'个';本回答被提问者采纳
第3个回答  2009-02-08
$arr = scandir($dir);
echo '共有 '.count($arr)." 个文件\n";
echo '.PHP文件个数:'.count(array_filter($arr,create_function('$val','return preg_match(\'/\\.php$/i\',$val);')))."个\n";
echo '非.TXT文件个数:'.count(array_filter($arr,create_function('$val','return preg_match(\'/(?<!\\.txt)$/i\',$val);')))."个\n";
第4个回答  2009-02-08
这样,比如你的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("/txt$/", $theFiles));//3、非txt文件个数
?>

主要就是opendir()函数和preg_grep()函数,像Unix里的正则.

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网