1

I have drafted a paragraph of text and each mailing log will generate a .log file

This is the code I refer to 1 user guide

function wh_log($log)
{
  $log_filename = "log";
  if (!file_exists($log_filename))
  {
    // create directory/folder uploads.
    mkdir($log_filename, 0777, true);
  }
  $log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
  file_put_contents($log_file_data, $log . "\n", FILE_APPEND);
}

The default data will be displayed every time overwriting

1
2
3
4
5

But I want the latest data to always be on the top

5
4
3
2
1

Please help me. Thanks all

  • https://www.php.net/manual/en/function.rsort.php ? – brombeer Sep 24 '19 at 09:37
  • quick'n'dirty solution: `file_put_contents($log_file_data, $log . "\n" . file_get_contents($log_file_data));` – Tox Sep 24 '19 at 09:39
  • just concatenate the payload first, then your log so it always prepend – Kevin Sep 24 '19 at 09:40
  • Woa Thank you @Tox I don't think it can be written like that. It's so good. Thank you very much – hiếu nguyễn Sep 24 '19 at 09:48
  • You're welcome. But mind you, that is not a very efficient solution. For small logs it's going to be fine, but large logs will slow it down quite a bit. – Tox Sep 24 '19 at 09:50
  • 1
    You can also do it like this: https://stackoverflow.com/questions/3332262/how-do-i-prepend-file-to-beginning#3332403 – freeek Sep 24 '19 at 09:59

0 Answers0