count the number of files in directory & its sub directories

Sun Mar 27, 2011 9:17 pm

A recursion counts to the number of files in a specific directory.
Code:


// folder path end with '/'
function directoryListCount($folderPath) {
 
   
 
    $num_files
= 0;
 
    $dirStream 
= opendir($folderPath);
 
    if 
(!$dirStream) return -1;
 
    while 
($file = readdir($dirStream)) {
 
        if 
($file == '.' || $file == '..') continue;
 
        if 
(is_dir($folderPath. $file)){      
            $num_files
+= directoryListCount($folderPath. $file . DIRECTORY_SEPARATOR);
        }
        else {
            $num_files++;  
        
}
    }
 
    closedir
($dirStream);
 
    return $num_files
;
}
 




  Related Posts  to : count the number of files in directory & its sub directories
 compute number of files in directory     -  
 Print all files in a directory     -  
 show all files in a directory with detail     -  
 Compare directories     -  
 recursive function to delete directories     -  
 convert integer number to octal,hexadecimal number systems     -  
 convert octal number to decimal number     -  
 convert decimal number to octal number     -  
 SQL COUNT Command     -  
 String char count     -  

Topic Tags

PHP Files and I/O