Archive

Posts Tagged ‘php total files’

php recursively count files under directory

August 24th, 2010 No comments

Sometimes(seems all times),virtual host provider has a limitation of file number on you(inode issue),and you want to use php to count files under directory.Here is a script for you.

many.php

FileCount("wordpress&rmbweb&liang99&huainong&discuz&cms&book",true);//true to count in files that positioned the same directory as this php script.First param is directory name.Use & to seperate from each param.
echo $count;

function FileCount($dir,$include=false){
global $count;
$arr=explode('&',$dir);
foreach($arr as $val){
if(is_dir($val)&&file_exists($val)){
$ob=scandir($val);
foreach($ob as $file){
if($file=="."||$file==".."){
continue;
}
$file=$val."/".$file;

if(is_file($file)){
$count++;
}elseif(is_dir($file)){
FileCount($file);
}
}
}
}
if($include){
$pathinfo=pathinfo(__FILE__);
$ob2=scandir($pathinfo['dirname']);
foreach($ob2 as $val){
if($val=='.'||$val=='..'){
continue;
}
$file=$pathinfo['dirname'].'/'.$val;
if(is_file($file)){
$count++;
}
}
}
}