Harlow
Harlow

Reputation: 153

Locate the latest version (by filename) of a specific file within a specific folder

Thanks in advance for your help...

Originally, I had a folder (named "reports") where an xml version of my the latest schedule for my app lived. The naming conventions for this xml file is "schedule-'currentdate'-'currenttime'.xml (ex: "schedule-20130313-070004.xml") I had no problem locating this file using scandir and sorting the folder contents in descending order and then taking the first.

My problem now is that another type of report has been added to the folder (ex: "roster-20130313-07004.xml") My former method no longer works. I'm outputting this information in two different places (schedule.xml on the schedule page, and roster.xml on the roster page) How can I search this folder, find the most current file, according to the date, and make sure that each report ends up where it's supposed to?

Upvotes: 0

Views: 69

Answers (1)

Sturm
Sturm

Reputation: 4285

Something simple such as

$files = scandir($dir);
$report = [];
$schedu = [];
foreach ( $files as $file ) {
    if ( strpos( $file, 'report') !== false ) {
        $report << $file;
    } else {
        $schedu << $file;
    }
}

//sort arrays

should work just fine. This will take the original array of directory results and place them into two new arrays, depending on whether or not the contain the string 'report'.

Upvotes: 1

Related Questions