0
/* function:  returns files from dir */
function get_files($images_dir,$exts = array('jpg')) {
    $files = array();
    if($handle = opendir($images_dir)) {
        while(false !== ($file = readdir($handle))) {
            $extension = strtolower(get_file_extension($file));
            if($extension && in_array($extension,$exts)) {
                $files[] = $file;
            }
        }
        closedir($handle);
    }
    return $files;
}

I have images named 01-image.jpg, 02-image.jpg etc but when I echo them out, they are in random order, how do I order them by name?

Matt
  • 15
  • 4

3 Answers3

1

before you echo them out use sort() - sort arrays in ascending order or rsort() - sort arrays in descending order

You can read more about sorting array here

// function to get img

$order = sort($img);
echo($order)
aaa
  • 836
  • 1
  • 23
  • 41
0

you can use sort with SORT_STRING option

http://php.net/manual/en/function.sort.php

sort($files, SORT_STRING);
shushu304
  • 1,403
  • 1
  • 5
  • 15
0

1) read the content of the directory to a variable (array)
2) sort the array as you need using any of those: Sorting Arrays
3) run your while to print the file names..

ino
  • 1,698
  • 1
  • 12
  • 21