The easiest way to get a dir listing and sort it is to exec() out to ls (eg:'ls -t'). But, that is considered "unsafe". My hosting company finally caught me doing it so here is my fastest solution. (Lucky for me each file is created with a Unix Timestamp at the end of the name and no other numbers in it.)
<?php
if ($handle = opendir('./images/motion_detection/')) {
$files=array();
while (false !== ($file = readdir($handle))) {
$files[$file] = preg_replace('/[^0-9]/', '', $file); }
closedir($handle);
arsort($files);
$files=array_keys($files);
}
?>
Before you go copying someone's bloat kitchen sink function/class, consider what you have and what you really need.