Search code examples
phpdateserverexif

Sorting image files using server modifed date. Not File EXIF modified


I have been told to rewrite my question by Stackoverflow

I upload images mostly to a directory on my sever where my website is hosted. Via a program on my PC and a app on phone and that works a treat.

I have come across PHP scripts that shows all the images in the above folder and returns a basic gallery, and they all work to a point.

What I am hoping to achieve (it might not be possible) is list the images by the date they were added to the server. Not sorting from the EXIF modified date.

If it cant be done, so be it.

Best regards,

RT

PS At the moment I'm use PHP Gallery, its OK but does not quite achive the sorting requirement I would like.

Gallery here. https://www.sidingstudios.com/pix4web/index.php

Is it possible to display the contents of server folder (images) but sort by Last Modified ? using PHP

My very basic script:
<?php
$images = glob('img/*');

foreach ($images as $image) {
  echo '<img src="'.$image.'"><br>';
}
?>

Link its used on. https://www.sidingstudios.com/pix4web2/


Solution

  • This will sort the images by last modified time

    $files = glob('img/*');
    
    $output = [];
    
    foreach ($files as $f) 
    {
        $output[filemtime($f)] = $f;
    }
    ksort($output);
    $images = array_reverse($output);
    
    foreach ($images as $image) 
    {
        echo '<img src="' . $image . '"><br />';
    }