0

I have a folder where i upload photos every day and they are displayed on a webpage from my site, like a gallery. The problem is that last uploaded photos are not shown in top, because they are sorted by name not by date.

Is it possible to sort them by date and after that to show them on the page?

<?php
                $files = (glob("../catalog-reseller/wall/diverse/*.jpg")); rsort($files);
                foreach (array_slice($files, 0) as $filename) 
                {   $x=$x+1;
                    echo'
                    <a href="'.$filename.'" data-rel="lightcase:gallery" title="Caption Text">
                    <img src="'.$filename.'" alt="">
                </a>';
                 if($x==50){break;}
                }
?>
Bogdan TB
  • 23
  • 4
  • Possible duplicate of [glob() - sort by date](https://stackoverflow.com/questions/124958/glob-sort-by-date) – Progrock Mar 11 '18 at 18:10

1 Answers1

0

You can see when a file was created using filectime($filename) and then use usort for custom sorting based of creating time. Here is what you would do if you want to show latest files first.

//usort expects a custom comparison function that takes two arguments
//custom function should return a number >= 1 if first_arg > second_arg
//custom function should return 0 if first_arg == second_arg
//and lastly, it should return a number < 0 if first_arg < second_arg

function compare($firstFile, $secondFile)
{
    return filectime($firstFile) - filectime($secondFile);
}

usort($files, "compare");

You can find the documentation for usort here , and the documentarion for filectime here.

QnARails
  • 377
  • 1
  • 4
  • 14