<?php
foreach (glob("POSTS/*.txt") as $filename)
{
$file = fopen($filename, 'r') or exit("Unable to open file!");
//Output a line of the file until the end is reached
echo date('D, M jS, Y H:i a', filemtime($filename))."<br>";
echo '<h2>' . htmlspecialchars(fgets($file)) . '</h2>';
while(!feof($file))
{
echo fgets($file). "<br>";
}
echo "<hr/>";
}
fclose($file);
?>
Im writing a blog and have been able to input the files but only by the oldest file first
how do i make it so that i input them by the newest date first?
Thanks
Use usort()
to put the file array in the right order (date desc).
function timeSort($first, $second) {
$firsttime = filemtime($first);
$secondtime = filemtime($second);
return $secondtime - $firsttime;
}
$files = glob("POSTS/*.txt");
usort($files, 'timeSort');
foreach ($files as $filename) {
/* Same as before */
}
If you really insist on using text files instead of a database to store your blog entries, one easy way to sort them in the desired way would be to use array_reverse()
, like the following:
$posts = array_reverse(glob("POSTS/*.txt"));
foreach($posts as $filename) {
// do your stuff
}