PHP count total files in directory AND subdirectory function

后端 未结 6 552
小蘑菇
小蘑菇 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 22:58

    You could do it like this using the RecursiveDirectoryIterator

    $cur) {
            $filesize=$cur->getSize();
            $bytestotal+=$filesize;
            $nbfiles++;
            $files[] = $filename;
        }
    
        $bytestotal=number_format($bytestotal);
    
        return array('total_files'=>$nbfiles,'total_size'=>$bytestotal,'files'=>$files);
    }
    
    $files = scan_dir('./');
    
    echo "Total: {$files['total_files']} files, {$files['total_size']} bytes\n";
    //Total: 1195 files, 357,374,878 bytes 
    ?>
    

提交回复
热议问题