The problem:
I'm trying to create a page which outputs images from a folder which I have been
able to do, but the problem I'm having is not being able to get the page to display
the most recent image according to file modification date at the top.
The first set of code below outputs the image timestamps in descending order, from newest to oldest which is
what I want, but as soon as I change/add a couple lines of code (Shown in the second lot of code) to get the image
file name along with the timestamp, the echoed list (timestamp and file names) gets muddled up in a random order.
In short;
As soon as the file names are retrieved with the timestamp, the list
goes from being organised descendingly, to not.
Show timestamp only code (1st lot of code):
<?php
//Open images directory
$ignore = array("..",".");
$dir = opendir("images1");
$images = array();
$sortedimages = array();
//List files in images directory
while (($file = readdir($dir)) !== false)
if (!in_array($file, $ignore))
$images[] = $file;
foreach ($images as $image)
{
$filetime = filemtime("images1/$image");
$sortedimages[] = $filetime;
}
rsort($sortedimages);
foreach ($sortedimages as $sorted)
{
//foreach ($sorted as $key => $value)
//{
echo "$sorted<br/>";
}
//}
closedir($dir);
?>
The show timestamp and file name code (2nd lot of code):
<?php
//Open images directory
$ignore = array("..",".");
$dir = opendir("images1");
$images = array();
$sortedimages = array();
//List files in images directory
while (($file = readdir($dir)) !== false)
if (!in_array($file, $ignore))
$images[] = $file;
foreach ($images as $image)
{
$filetime = filemtime("images1/$image");
$sortedimages[] = array($filetime => $image);
}
rsort($sortedimages);
foreach ($sortedimages as $sorted)
{
foreach ($sorted as $key => $value)
{
echo "$key and $value<br/>";
}
}
closedir($dir);
?>
The changes made in the 2nd script from the 1st:
//Changed:
$sortedimages[] = $filetime; ---> $sortedimages[] = array($filetime => $image);
//Included the previously commented out:
foreach ($sorted as $key => $value)
{
}
//Changed:
echo "$sorted<br/>"; ---> echo "$key and $value<br/>";
Thanks for any help!