Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count the Number of Files in a Directory
#1
Credits and Source: http://www.dreamincode.net/

Name : Count the Number of Files in a directory
Description: Get the file counts from the directory including the files of the subfolders...
Snippet:
PHP Code:
<?
function filecount($FolderPath) {
    
    
$filescount 0;
 
// Open the directory 
    
$dir opendir($FolderPath);
    
// if the directory doesn't exist  return 0
    
if (!$dir){return 0;}
 
// Read the directory and get the files 
    
while (($file readdir($dir)) !== false) {
 
        if (
$file[0] == '.'){ continue; }
        
         
//if '.' it is a sub folder and call the function recursively
        
if (is_dir($FolderPath.$file)){        
            
            
// Call the function if it is a folder type
            
$filescount += filecount($FolderPath.$file.DIRECTORY_SEPARATOR);            
        }
        else {
            
// Increment the File Count.
            
$filescount++;
        }
    }    
    
// close the directory
    
closedir($dir);
     return 
$filescount;
}

//add slash at the end of your page
$FolderPath='/usr/local/apache/htdocs/test/';
$filecountfilecount($FolderPath);
print 
"Your Total Files : ".$filecount;

?>

Thats it, thankyou for reading and be happy always Smile
Reply


Messages In This Thread
Count the Number of Files in a Directory - by zone - 11-06-2009, 06:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete directory and all content zone 0 666 11-08-2009, 08:50 AM
Last Post: zone
  Directory Contents zone 0 567 11-07-2009, 03:38 AM
Last Post: zone
  Directory Contents zone 0 564 11-06-2009, 07:12 AM
Last Post: zone

Forum Jump:


Users browsing this thread: 1 Guest(s)