I needed a function that would sort a list of files into reversed order based on their modification date.
Here's what I came up with:
function display_content($dir,$ext){
$f = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($folder = readdir($dh)) !== false) {
if (preg_match("/\s*$ext$/", $folder)) {
$fullpath = "$dir/$folder";
$mtime = filemtime ($fullpath);
$ff = array($mtime => $fullpath);
$f = array_merge($f, $ff);
}
}
rsort($f, SORT_NUMERIC);
while (list($key, $val) = each($f)) {
$fcontents = file($val, "r");
while (list($key, $val) = each($fcontents))
echo "$val\n";
}
}
}
closedir($dh);
}
Call it like so:
display_content("folder","extension");