Jump to content

Detect newest file in a folder with multiple files and echo file to page?


schris403

Recommended Posts

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.. What 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.. 

Thanks!!
Chris

[code<?php
$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";
}
?> 

Sorry for screwing up the code brackets, here is my message again. 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..

 

Thanks!!

Chris

 

<?php
$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";
}
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.