I have a directory full of files that I am trying to echo out. If the file is an image, the image itself is echoed out. If the file is not an image, the name of the file is echoed out.
This code below works perfectly however I can't seem to get the order sorted by date. The files are randomly echoed out.
How would I make it so that the files are sorted by last modified (latest first).
<?php
$blacklist = array("index.php");
$ext = pathinfo($files, PATHINFO_EXTENSION);
if ($handle = opendir('.')) {
$valid_image = array("jpg", "jpeg", "png", "gif");
while (false !== ($entry = readdir($handle))) {
krsort($entry);
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
$exploded = explode('.', $entry);
if(in_array(end($exploded), $valid_image))
{
echo "<div><h4>"; echo date('d F Y', filemtime($file)) . "</h4><a href='" . $entry . "'><img src='".$entry."'></a></div><hr>";
}
else
{
echo "<div><h4>"; echo date('d F Y', filemtime($file)) . "</h4><a href='" . $entry . "'>" . $entry . "</a></div>";
}
}
}
closedir($handle);
}
?>