Single file Gallery display PHP code

Posted on February 17th, 2025
Share this post:

Single file Gallery display PHP code

Image Gallery

Image Gallery

function displayItems($dir) {
$folders = [];
$images = [];
$items = array_diff(scandir($dir), array('..', '.'));
usort($items, function($a, $b) use ($dir) {
return filemtime($dir . '/' . $b) - filemtime($dir . '/' . $a);
});
foreach ($items as $item) {
$itemPath = $dir . '/' . $item;
if (is_dir($itemPath)) {
$folders[] = $item;
} elseif (preg_match('/.(jpg|jpeg|png|gif)$/i', $item)) {
$images[] = $item;
}
}
foreach (array_merge($images, $folders) as $item) {
$itemPath = $dir . '/' . $item;
if (is_dir($itemPath)) {
echo '
echo '
📁
';
echo '

' . $item . '

';
echo '
';
} elseif (preg_match('/.(jpg|jpeg|png|gif)$/i', $item)) {
$fileName = pathinfo($item, PATHINFO_FILENAME);
echo '
echo 'Gallery Image';
echo '

' . $fileName . '

';
echo '
';
}
}
}
$rootDir = isset($_GET['folder']) ? $_GET['folder'] : 'images';
displayItems($rootDir);
?>
Overlay Image

Category:
IT , Technical Hacks etc

Posted on:
February 17th, 2025