Forum Moderators: coopster
<?php
$path = "docs/";
// show the most recent file
echo "Most recent file is: ".getNewestFN($path);
// Returns the name of the newest file
// (My_name YYYY-MM-DD HHMMSS.inf)
function getNewestFN ($path) {
// store all .inf names in array
$p = opendir($path);
while (false !== ($file = readdir($p))) {
if (strstr($file,".inf"))
$list[]=date("YmdHis ", filemtime($path.$file)).$path.$file;
}
// sort array descending
rsort($list);
// return newest file name
return $list[0];
}
?>
The function is_dir will always return false if the handle acquired with opendir is not from the current working directory (getcwd); exception applies to "." and "..". Thus, if you need a consistent dir listing from any directory other than the current one, you must change dir first.
<?php
function getNewestDir($path) {
$working_dir = getcwd();
chdir($path); ## chdir to requested dir
$ret_val = false;
if ($p = opendir($path) ) {
while (false !== ($file = readdir($p))) {
if ($file{0} != '.' && is_dir($file)) {
$list[] = date('YmdHis', filemtime($path.'/'.$file)).$path.'/'.$file;
}
}
rsort($list);
$ret_val = $list[0];
}
chdir($working_dir); ## chdir back to script's dir
return $ret_val;
}
?>