How is it possible to count all the directories and subdirectories, but no files?

    Well where have you got to so far?

    You open the directory with opendir()

    Get the contents with readdir()

    Check if its a directory with is_dir()

    So you could do something like this:

    <?php
    $dir = '' // the directory
    $num_dirs = 0;
    
      if ($dir = opendir($dir))
                {
                    while (($file = readdir($dir)) !== false)
                    {
                        if(is_dir($file) AND $file != "." AND $file != "..")
                        {
                            $num_dirs++;
                        }
                    }  
    closedir($dir);
    $total = $num_dirs; } ?>

      You need a recursive function like this...

      <?php
      function dirCount($strDir){
      	$intNB	= 0;
      	$strDir	= str_replace('\\', '/', $strDir);
      	$strDir	= rtrim($strDir, '/');
      	$resDir	= opendir($strDir);
      	while(($strFile = readdir($resDir)) !==false) {
      		if(($strFile <> '.') && ($strFile <> '..') && is_dir($strDir . '/' . $strFile)) {
      			$intNB++;
      			$intNB	+= dirCount($strDir . '/' . $strFile);
      		}
      	}
      	return $intNB;
      }
      echo dirCount('/home'); // just an example
      ?>

        You might find it useful to use [man]glob/man with the GLOB_ONLYDIR option, rather than the opendir()/readdir()/is_dir() combination.

          Write a Reply...