• <i id='dnUZd'><tr id='dnUZd'><dt id='dnUZd'><q id='dnUZd'><span id='dnUZd'><b id='dnUZd'><form id='dnUZd'><ins id='dnUZd'></ins><ul id='dnUZd'></ul><sub id='dnUZd'></sub></form><legend id='dnUZd'></legend><bdo id='dnUZd'><pre id='dnUZd'><center id='dnUZd'></center></pre></bdo></b><th id='dnUZd'></th></span></q></dt></tr></i><div id='dnUZd'><tfoot id='dnUZd'></tfoot><dl id='dnUZd'><fieldset id='dnUZd'></fieldset></dl></div>

    <small id='dnUZd'></small><noframes id='dnUZd'>

      <bdo id='dnUZd'></bdo><ul id='dnUZd'></ul>
    <tfoot id='dnUZd'></tfoot>
    1. <legend id='dnUZd'><style id='dnUZd'><dir id='dnUZd'><q id='dnUZd'></q></dir></style></legend>

        从文件中读取最后一行

        Read last line from file(从文件中读取最后一行)

        • <tfoot id='HsWc7'></tfoot>

        • <small id='HsWc7'></small><noframes id='HsWc7'>

            <bdo id='HsWc7'></bdo><ul id='HsWc7'></ul>

            <legend id='HsWc7'><style id='HsWc7'><dir id='HsWc7'><q id='HsWc7'></q></dir></style></legend>
              <i id='HsWc7'><tr id='HsWc7'><dt id='HsWc7'><q id='HsWc7'><span id='HsWc7'><b id='HsWc7'><form id='HsWc7'><ins id='HsWc7'></ins><ul id='HsWc7'></ul><sub id='HsWc7'></sub></form><legend id='HsWc7'></legend><bdo id='HsWc7'><pre id='HsWc7'><center id='HsWc7'></center></pre></bdo></b><th id='HsWc7'></th></span></q></dt></tr></i><div id='HsWc7'><tfoot id='HsWc7'></tfoot><dl id='HsWc7'><fieldset id='HsWc7'></fieldset></dl></div>

                    <tbody id='HsWc7'></tbody>
                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.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                      <tbody id='q1686'></tbody>

                      <bdo id='q1686'></bdo><ul id='q1686'></ul>
                      <i id='q1686'><tr id='q1686'><dt id='q1686'><q id='q1686'><span id='q1686'><b id='q1686'><form id='q1686'><ins id='q1686'></ins><ul id='q1686'></ul><sub id='q1686'></sub></form><legend id='q1686'></legend><bdo id='q1686'><pre id='q1686'><center id='q1686'></center></pre></bdo></b><th id='q1686'></th></span></q></dt></tr></i><div id='q1686'><tfoot id='q1686'></tfoot><dl id='q1686'><fieldset id='q1686'></fieldset></dl></div>
                    • <legend id='q1686'><style id='q1686'><dir id='q1686'><q id='q1686'></q></dir></style></legend>
                      <tfoot id='q1686'></tfoot>
                        • <small id='q1686'></small><noframes id='q1686'>