I have been working on the following script for a few days now trying to make it work the way I want and I just can't seem to get it to completely work correct. What it does is the following:
Check if file1.txt or file2.txt exists, if so, get when file1.txt was last modified, if it was later then 35 minutes ago, don't do anything and go on to check if file2.txt exists and go through the same process again.
I eventually want to add file_3, file_4, etc, is there a simplified way to write this code below to make this work?
The problem I am having with the below code is if the file_1 doesn't exist, it doesn't move on to file_2, it just ends saying both files don't exist. I guess I could just copy the file_2 if file exists statement again down below but I feel like I am just missing something that would work much better.
Any help is greatly appreciated.
Thanks!
Chris
<?php
$file_1 = "file1.txt";
$file_2 = "file2.txt";
$blank_file = 'blank.txt';
if (file_exists($file_1)) {
$file_time = filemtime("$file_1");
$last_modified = date("Y-m-d H:i:s", $file_time);
$file_name = date("Y-m-d", $file_time);
$yesterday = date("Y-m-d H:i:s", strtotime("35 minutes ago"));
if ($last_modified > $yesterday) {
$xml = file_get_contents ($file_1);
if (preg_match("#</(ending1|ending2)>#", $xml)) {
echo $xml;
} else {
$blank = file_get_contents ($blank_file);
echo $blank;
}
} else {
if (file_exists($file_2)) {
$file_time = filemtime("$file_2");
$last_modified = date("Y-m-d H:i:s", $file_time);
$file_name = date("Y-m-d", $file_time);
$yesterday = date("Y-m-d H:i:s", strtotime("35 minutes ago"));
if ($last_modified > $yesterday) {
$xml = file_get_contents ($file_2);
if (preg_match("#</(ending1|ending2)>#", $xml)) {
echo $xml;
} else {
$blank = file_get_contents ($blank_file);
echo $blank;
}
} else {
echo "File 2 is too old";
}
} else {
echo "File 2 does not exist";
}
}
} else {
echo "Both files do not exist";
}
?>