PHP count total files in directory AND subdirectory function

后端 未结 6 557
小蘑菇
小蘑菇 2021-01-18 22:36

I need to get a total count of JPG files within a specified directory, including ALL it\'s subdirectories. No sub-sub directories.

Structure looks like this :

<
6条回答
  •  花落未央
    2021-01-18 23:16

    error_reporting(E_ALL);
    
    function printTabs($level)
    {
        echo "

    "; $l = 0; for (; $l < $level; $l++) echo "."; } function printFileCount($dirName, $init) { $fileCount = 0; $st = strrpos($dirName, "/"); printTabs($init); echo substr($dirName, $st); $dHandle = opendir($dirName); while (false !== ($subEntity = readdir($dHandle))) { if ($subEntity == "." || $subEntity == "..") continue; if (is_file($dirName . '/' . $subEntity)) { $fileCount++; } else //if(is_dir($dirName.'/'.$subEntity)) { printFileCount($dirName . '/' . $subEntity, $init + 1); } } printTabs($init); echo($fileCount . " files"); return; } printFileCount("/var/www", 0);

    Just checked, it's working. But the alignment of results is bad,logic works

提交回复
热议问题