How can PHP be used to calculate the total size of images in a folder?

To calculate the total size of images in a folder using PHP, you can iterate through each file in the folder, check if it is an image file, and then sum up the sizes of all image files found.

<?php
$folder = 'path/to/folder';
$totalSize = 0;

$files = scandir($folder);

foreach($files as $file){
    $filePath = $folder . '/' . $file;
    
    if(is_file($filePath) && getimagesize($filePath)){
        $totalSize += filesize($filePath);
    }
}

echo "Total size of images in folder: " . $totalSize . " bytes";
?>