How is it possible to count all the directories and subdirectories, but no files?
counting directories and subdirectories (no files)
You can check if a filename is a directory or not using is_dir
I know, but how do I count the dirs?
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
?>