I have created a directory with the following files located within:
- index.php
- one.txt - Hello
- two.txt - Ok
- three.txt - Goodbye
- four.txt - Cool
Everything in bold tells you what those text files contain.
What I am trying to do is echo out all of the contents of the text files into the index.php page. So when the user visits the index.php page, this is what they'll see:
Date: 13 May 2015
Contents of text file: HelloDate: 12 May 2015
Contents of text file: OkDate: 11 May 2015
Contents of text file: GoodbyeDate: 10 May 2015
Contents of text file: Cool
As you can see from above, the date the text files were created along with its contents are all echoed out. Also, they are echoed out based on the order that they were last modified.
This is the code that I am trying to use to achieve this:
<?php
foreach (glob("*.txt") as $filename) {
echo "Date:";
echo date('d F Y', filemtime($filename)) .
"Contents of text file:";
echo file_get_contents($filename);
}
?>
What's happening in this code is that:
- All of the text files in the directory are picked up
- For each text file, it gets its last modification date and what it contains echoed out
The outcome of this code is that it is similar to what can be seen in the yellow box above (which is what I am trying to achieve) however the order of the echo is not in date order. It gets echoed out a little something like this:
- 13 May
- 10 May
- 11 May
- 12 May
How would I make it so that it gets echoed out based on the date that it was last modified? With the latest date at the top and the oldest date at the bottom?