Logfile PHP [PHP]

65 views Asked by At

I am currently trying to do a logfile where I wanna insert a reverse chronology style (newest items will be put on top of the list of all existing texts, and previous existing items will be pushed below), think of it like Facebook Page, where if you visit a Profile Page, you can see all his posts by latest -> oldest, instead of currently what I have is, oldest -> latest. Here's my code so far;

    $date = date("d-m-Y H:i:s");
    $file = 'logfile.txt';
    if (file_exists($file)) {
        $current = file_get_contents($file);
        $current .= PHP_EOL.$date;
        file_put_contents($file, $current);
    } else {
        file_put_contents($file, $date);
    }  
1

There are 1 answers

0
jeroen On BEST ANSWER

You are adding your date string to the end of the variable:

$current .= PHP_EOL.$date;

You need to start with the newest and add the older entries after it:

$current = $date . PHP_EOL . $current;