phpSensei Posted September 29, 2007 Share Posted September 29, 2007 How do you count the number of folders in a directory? Quote Link to comment https://forums.phpfreaks.com/topic/71127-count-number-of-folders/ Share on other sites More sharing options...
teng84 Posted September 29, 2007 Share Posted September 29, 2007 foreach (glob("*") as $filename) { echo "$filename size " . filesize($filename) . "\n";++$ctr; } echo $ctr; //or <?php // Note that !== did not exist until 4.0.0-RC2 if ($handle = opendir('/path/to/files')) { echo "Directory handle: $handle\n"; echo "Files:\n"; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { echo "$file\n "; ++$ctr; } echo $ctr; closedir($handle); } or http://www.php.net/manual/en/function.opendir.php theres actually lots of way of doing this stuff Quote Link to comment https://forums.phpfreaks.com/topic/71127-count-number-of-folders/#findComment-357716 Share on other sites More sharing options...
markjoe Posted September 29, 2007 Share Posted September 29, 2007 In PHP5 <?php $dirs=0; $x="."; $y=scandir($x); foreach($y as $z){ if(is_dir($z)){ $dirs++; } } echo "There are <b>$dirs</b> in directory $x"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71127-count-number-of-folders/#findComment-357754 Share on other sites More sharing options...
animico Posted March 2, 2011 Share Posted March 2, 2011 I know this post is very old but it may be useful for someone. $folder = 'folder1/folder2'; echo (count(glob("$folder/*",GLOB_ONLYDIR))); Quote Link to comment https://forums.phpfreaks.com/topic/71127-count-number-of-folders/#findComment-1181746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.