How can the user modify the code to dynamically handle images from different subfolders within the "photos" directory?

The user can modify the code by using a loop to iterate through all subfolders within the "photos" directory and dynamically load images from each subfolder. This can be achieved by using the PHP opendir() function to open the "photos" directory, then iterating through each subfolder using readdir() and loading images from each subfolder.

<?php
$dir = "photos";
$dh  = opendir($dir);

while (false !== ($subfolder = readdir($dh))) {
    if ($subfolder != '.' && $subfolder != '..') {
        $subfolder_path = $dir . '/' . $subfolder;
        $files = glob("$subfolder_path/*.jpg");
        
        foreach ($files as $file) {
            echo '<img src="' . $file . '" alt="' . basename($file) . '">';
        }
    }
}

closedir($dh);
?>