Jump to content

Simple count files


birman

Recommended Posts

Got bored, so i put this together:

 

 

<?php
function no_of_files($start_dir,$sub_dir=FALSE){
	$no_of_files=0;
	if($sub_dir === false){
	$handler = opendir($start_dir);	
}else{
 	$start_dir = $start_dir.$sub_dir;
	$handler = opendir($start_dir);
}

while(false !== ($file = readdir($handler))){
	if($file != '.' && $file != '..'){
		if(is_dir($start_dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined
			$dir_name = '\\'.$file;
			$no_of_files += no_of_files($start_dir,$dir_name);	
		}else{//is a file, so increase our counter
			$no_of_files++;	
		}	
	}
}
return $no_of_files;
}
echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music');
?>

 

Tested and compared with the number of files reported by windows. Seems to work fine to me.

Link to comment
https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293409
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.