glob() — Sort by Name

You can use array_reverse:

foreach(array_reverse(glob("*.txt")) as $filename) { ...

Just a addition to @Foo Bah's answer : When dealing with file names in a directory, I usually add natsort to prevent the typical ordering case :

  • 'image1.png'
  • 'image10.png'
  • 'image2.png'

natsort is a more user friendly sorting algorithm that will preserve natural numbering :

  • 'image1.png'
  • 'image2.png'
  • 'image10.png'

So FooBah's answer becomes :

$list = glob("*.jpg");
natsort($list);
foreach(array_reverse($list) as $filename) { ...

Please note that natsort is modifying the array passed in parameter and only returns a boolean.