日志文件 PHP [PHP]
Logfile PHP [PHP]
我目前正在尝试做一个日志文件,我想在其中插入一个反向时间顺序样式(最新的项目将放在所有现有文本列表的顶部,而以前的现有项目将被推到下面),想一想比如 Facebook 页面,如果你访问个人资料页面,你可以看到他的所有帖子,按最新 -> 最旧,而不是我目前拥有的,最旧 -> 最新。到目前为止,这是我的代码;
$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);
}
您正在将日期字符串添加到变量的末尾:
$current .= PHP_EOL.$date;
您需要从最新的条目开始,然后添加较旧的条目:
$current = $date . PHP_EOL . $current;
我目前正在尝试做一个日志文件,我想在其中插入一个反向时间顺序样式(最新的项目将放在所有现有文本列表的顶部,而以前的现有项目将被推到下面),想一想比如 Facebook 页面,如果你访问个人资料页面,你可以看到他的所有帖子,按最新 -> 最旧,而不是我目前拥有的,最旧 -> 最新。到目前为止,这是我的代码;
$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);
}
您正在将日期字符串添加到变量的末尾:
$current .= PHP_EOL.$date;
您需要从最新的条目开始,然后添加较旧的条目:
$current = $date . PHP_EOL . $current;