問題描述
我遇到了一個問題.我在 Linux 機器上有一個日志,其中寫入了幾個正在運行的進程的輸出.這個文件有時會變得非常大,我需要讀取該文件的最后一行.
I've been bumping into a problem. I have a log on a Linux box in which is written the output from several running processes. This file can get really big sometimes and I need to read the last line from that file.
問題是此操作將經常通過 AJAX 請求調用,并且當該日志的文件大小超過 5-6MB 時,對服務器來說相當不利.所以我想我必須閱讀最后一行,而不是閱讀整個文件并通過它或將它加載到 RAM 中,因為那只會加載到我的盒子里.
The problem is this action will be called via an AJAX request pretty often and when the file size of that log gets over 5-6MB it's rather not good for the server. So I'm thinking I have to read the last line but not to read the whole file and pass through it or load it in RAM because that would just load to death my box.
這個操作有沒有什么優化,可以流暢運行,不傷服務器,不殺Apache?
Is there any optimization for this operation so that it run smooth and not harm the server or kill Apache?
我的其他選擇是 exec('tail -n 1/path/to/log')
但聽起來不太好.
Other option that I have is to exec('tail -n 1 /path/to/log')
but it doesn't sound so good.
稍后我不想將文件放在 RAM 中,因為它可能會變得很大.fopen()
不是一個選項.
Later edit: I DO NOT want to put the file in RAM because it might get huge. fopen()
is not an option.
推薦答案
這應該有效:
$line = '';
$f = fopen('data.txt', 'r');
$cursor = -1;
fseek($f, $cursor, SEEK_END);
$char = fgetc($f);
/**
* Trim trailing newline chars of the file
*/
while ($char === "
" || $char === "
") {
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
/**
* Read until the start of file or first newline char
*/
while ($char !== false && $char !== "
" && $char !== "
") {
/**
* Prepend the new char
*/
$line = $char . $line;
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
fclose($f);
echo $line;
請注意,除非您的文件以換行符結尾,否則此解決方案將重復該行的最后一個字符.如果您的文件沒有以換行符結尾,您可以將 $cursor--
的兩個實例更改為 --$cursor
.
Note that this solution will repeat the last character of the line unless your file ends in a newline. If your file does not end in a newline, you can change both instances of $cursor--
to --$cursor
.
這篇關于從文件中讀取最后一行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!