-1

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?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Your `$fname` string at the bottom is returning just for the month of May now, as `date('m')` will print the current month. We need to decide a better way to list the files in XML, or how to determine which month's files we want to find. – dcclassics May 01 '14 at 15:26

2 Answers2

1

I assume the filenames are of the format "201404.xml" where the filename is the year+month? If so, can't you read the files into an array and sort it, or even better if you just want the latest, read all the files and compare the current name to the highest, if it's greater then set the highest to the current name and get the next file, looping until you get to the end of the list. You need to set highest to blanks (or null before you start.

Tony Payne
  • 382
  • 2
  • 10
  • 1
    Or check if 201405.xml is empty, instead look for `date('m') - 1`. – dcclassics May 01 '14 at 15:34
  • If I have to read all the files in a folder I usually do a directory list of the folder and then import that to a database table. You could then use SQL to select the file with the highest value name. Might be overkill in this case, but in ours we need to store all the image names in a folder, and it's faster to do that than to read through 12,000+ files. – Tony Payne May 01 '14 at 15:44
1

I was able to add a variable with the help a a programmer:

if(date('m')-1>9)
$ss=date('m')-1;
else
$ss="0".(date('m')-1);

$filee=$path."/".date('Y').date('m').".xml";
if(file_exists($filee))
$fname=$path."/".date('Y').date('m').".xml";
else
$fname=$path."/".date('Y').($ss).".xml";

Seems to work now!