从文件中读取最后一行

2023-05-30php开发问题
1

本文介绍了从文件中读取最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了一个问题.我在 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.

这篇关于从文件中读取最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17