Switch to full style
PHP examples
Post a reply

count the number of files in directory & its sub directories

Sun Mar 27, 2011 10: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
;
}
 




Post a reply
  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     -  
 count elements in a vector     -  

Topic Tags

PHP Files and I/O