Automatically list images in a directory with PHP

Sometimes I want to lob some images into a directory to be viewed online for review.
There is a way of doing this using glob which is a function in PHP that searches for path names matching patterns according to specified rules.
<?php
echo('<style type="text/css">
img {
margin:3px;
padding: 7px;
border: 1px solid #aaa;
background: #eee;
display: inline-block;
}
p {
}
</style>');
//specify the directory
$latestfiles = glob("images/*.*");
echo('<h1>Latest</h1>');
for ($i=1; $i<count($latestfiles); $i++) {
$image = $latestfiles[$i];
echo '<p><img src="'.$image .'" alt="Random image"/>'."<br/>URL: <a href=\"".$image."\">".$image.'</a></p>';
?>
This codes allows me to show all files within a directory and can even be expanded to make a basic file manager.

Leave a Reply