PHP 8.5.0 Alpha 4 available for testing

Voting

: nine minus five?
(Example: nine)

The Note You're Voting On

Richard Bronosky
16 years ago
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
#exec('ls -t ./images/motion_detection/', $files); # They outlawed exec, so now I have to do it by hand.
if ($handle = opendir('./images/motion_detection/')) {
$files=array();
while (
false !== ($file = readdir($handle))) {
$files[$file] = preg_replace('/[^0-9]/', '', $file); # Timestamps may not be unique, file names are.
}
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.

<< Back to user notes page

To Top