View Full Version : Detect newest filename in a folder with multiple files?

03-13-2008, 03:51 AM
I am in a very tight time crunch to get this working, please if there is anyone who could help me with this I would GREATLY appreciate it. I have searched all over before I have posted to see if I could find a similar example but can't find any.. That I need to do is to have a php script scan a folder of about 12 files, detect by date/time modified which one is the newest and echo that files contents back. I already have most of that done, just need to figure out how to detect the newest filename. What I need to incorporate this into is the code below which currently only reads from one preset file. All that needs to be done is the filename variable needs to be determined based on newest xml file in the folder, which could have a variety of names, in the same folder this php script is located. Thank you so much to anyone who can help me out with this, this has to go live very early in the morning..


$filename = 'file.xml';
$blank_file = 'blank.xml';

if (file_exists($filename)) {
$xml = file_get_contents ($filename);
if (preg_match("#</(text1|text2)>#", $xml)) {
echo $xml;
} else {
$blank = file_get_contents ($blank_file);
echo $blank;
} else {
$blank = file_get_contents ($blank_file);
echo "$blank";

03-13-2008, 05:19 AM
Without doing any testing, I know that filemtime() (http://php.net/filemtime) is what you're looking for.