I created a PHP script to find the latest XML file in a directory, and it worked great, but today is May 1st and my latest file is from April 30th. Today the result shows "file not found", but it is there. There is something wrong with my method of finding the last file and it having a date stamp that is not the current month.
Here is my code:
// Initialize list arrays, files and array counters for them
$t = 0;
$f = 0;
$files_arr['name'] = array();
$files_arr['time'] = array();
if (@$handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fName = $file;
$file = $path . '/' . $file;
if (is_file($file)) {
/* here is the sorting by date, just a seperate key
in the array to store filetimes */
$files_arr['time'][$t++] = filemtime($file);
$files_arr['name'][$f++] = $file;
}
;
}
;
}
;
closedir($handle);
asort($files_arr['time']);
asort($files_arr['name']);
}
//test
foreach ($files_arr['time'] as $key => $ftime) {
$fname = $files_arr['name'][$key];
}
// End Finding Latest File in Dir
// $source = file_get_contents('data/201404.xml');
// echo $fname;exit;
$fname = $path . "/" . date('Y') . date('m') . ".xml";
Can anyone help me just get the latest file and not have it dependent on the current date?