How can the use of PHP scripts be optimized to efficiently display images from various subfolders?
To efficiently display images from various subfolders using PHP scripts, you can use a recursive function to scan through the directories and retrieve the image files. This way, you can dynamically load images from multiple subfolders without hardcoding each folder path.
<?php
function displayImagesFromSubfolders($dir) {
$files = scandir($dir);
foreach($files as $file) {
if ($file != '.' && $file != '..') {
if (is_dir($dir . '/' . $file)) {
displayImagesFromSubfolders($dir . '/' . $file);
} else {
if (pathinfo($file, PATHINFO_EXTENSION) == 'jpg' || pathinfo($file, PATHINFO_EXTENSION) == 'png') {
echo '<img src="' . $dir . '/' . $file . '" alt="' . $file . '">';
}
}
}
}
}
$rootDir = 'images';
displayImagesFromSubfolders($rootDir);
?>