Verification: a143cc29221c9be0

Php all images from folder

Topic

Php - Displaying Images From Folder, With Most Recent First

View Content

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):

//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
";

}

//}

closedir($dir);

?>

The show timestamp and file name code (2nd lot of code):

//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
";

}

}

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
"; ---> echo "$key and $value
";

Thanks for any help!