thebluebus Posted March 22, 2008 Share Posted March 22, 2008 hi, wondered if you may be able to help me. I'm trying to write a php script to count the number of files within a folder and its subdirectories <?php $count = 0; foreach( glob( "images/*.*" ) as $filename ) { $count++; } echo $count; ?> but it only counts files in the one folder. How do i make it include subdirectories? thanks Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/ Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 Haven't tested this, but I think it'd work: <?php //$dir, a directory name ending with a "/" function count_files ($dir) { $count = 0; if(!is_dir($dir)) return FALSE; $h = opendir($dir); while($file = readdir($h)) { if($file == "." || $file == "..") continue; if(is_dir($file) $count += count_files($dir.$file."/"); else $count++; } closedir($h); return $count; } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498146 Share on other sites More sharing options...
thebluebus Posted March 22, 2008 Author Share Posted March 22, 2008 thanks orio, where do i put the address of the folder it should search? Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498148 Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 It's a function... You call it with the dir's name: <?php $num_images = count_files("images/"); echo "There are ".$num_images." images!"; //$dir, a directory name ending with a "/" function count_files ($dir) { $count = 0; if(!is_dir($dir)) return FALSE; $h = opendir($dir); while($file = readdir($h)) { if($file == "." || $file == "..") continue; if(is_dir($file) $count += count_files($dir.$file."/"); else $count++; } closedir($h); return $count; } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498153 Share on other sites More sharing options...
thebluebus Posted March 22, 2008 Author Share Posted March 22, 2008 thanks again. i'm getting an error though Parse error: syntax error, unexpected T_VARIABLE in /mounted-storage/home34b/sub001/sc27915-YPZT/1234.com/header.php on line 34 line 34 is $count += count_files($dir.$file."/"); Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498158 Share on other sites More sharing options...
Kieran Menor Posted March 22, 2008 Share Posted March 22, 2008 I have another candidate that I just made up: <?php function count_deep($folder, $filetype = "*", $count_folders = false) { $c = 0; $dirs = array($folder); while($dir = each($dirs)) { foreach(glob($dir[1]."/*", GLOB_ONLYDIR) as $filename) { $dirs[] = $filename; } $c += count(glob($dir[1]."/".$filetype)); } if(!$count_folders && ($filetype == "*")) $c -= (count($dirs)-1); return $c; } echo count_deep("images"); ?> Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498161 Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 I forgot to close a bracket: <?php $num_images = count_files("images/"); echo "There are ".$num_images." images!"; //$dir, a directory name ending with a "/" function count_files ($dir) { $count = 0; if(!is_dir($dir)) return FALSE; $h = opendir($dir); while($file = readdir($h)) { if($file == "." || $file == "..") continue; if(is_dir($file)) $count += count_files($dir.$file."/"); else $count++; } closedir($h); return $count; } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498163 Share on other sites More sharing options...
thebluebus Posted March 22, 2008 Author Share Posted March 22, 2008 ok, error is now gone, but the script is counting the number of files+folders in a directory and giving a number. e.g. 2 folders + 1 file = "There are 3 images" I may have been unclear in my first post. I want it to count the total number of files in all the subdirectories within the specified directory Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498166 Share on other sites More sharing options...
Kieran Menor Posted March 22, 2008 Share Posted March 22, 2008 Did you try my function? Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498167 Share on other sites More sharing options...
thebluebus Posted March 22, 2008 Author Share Posted March 22, 2008 I have another candidate that I just made up: <?php function count_deep($folder, $filetype = "*", $count_folders = false) { $c = 0; $dirs = array($folder); while($dir = each($dirs)) { foreach(glob($dir[1]."/*", GLOB_ONLYDIR) as $filename) { $dirs[] = $filename; } $c += count(glob($dir[1]."/".$filetype)); } if(!$count_folders && ($filetype == "*")) $c -= (count($dirs)-1); return $c; } echo count_deep("images"); ?> thanks a lot! works perfectly Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498168 Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 Found my error. Works now. <?php $num_images = count_files("images/"); echo "There are ".$num_images." images!"; //$dir, a directory name ending with a "/" function count_files ($dir) { $count = 0; if(!is_dir($dir)) return FALSE; $h = opendir($dir); while($file = readdir($h)) { if($file == "." || $file == "..") continue; if(is_dir($dir.$file)) $count += count_files($dir.$file."/"); else $count++; } closedir($h); return $count; } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/#findComment-498175 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.