This question already has an answer here:
/* 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?
</div>
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)
you can use sort with SORT_STRING option
http://php.net/manual/en/function.sort.php
sort($files, SORT_STRING);
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..