Use file_scan_directory and sort files in array

If you are using file_scan_directory to scan files in some directory and then loop over them to use them for some action, you probably also want to sort them, otherwise you will get them in order that is set in filesystem. So to do that, you need to set key in options to "filename" to use filename as key, so you can sort that associative array per key. Use "ksort" to sort this array and then you can use $file_names where keys are sorted ascending.

$options['key'] = "filename";
$file_names = file_scan_directory($value, '/.*\.(png|jpg)$/', $options);