Read last line from file

后端 未结 12 1676
南方客
南方客 2020-12-01 09:13

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 ne

相关标签:
12条回答
  • 2020-12-01 09:53

    If you know the upper bound of line length you could do something like this.

    $maxLength = 1024;
    $fp = fopen('somefile.txt', 'r');
    fseek($fp, -$maxLength , SEEK_END); 
    $fewLines = explode("\n", fgets($fp, $maxLength));
    $lastLine = $fewLines[count($fewLines) - 1];
    

    In response to the edit: fopen just acquires a handle to the file (i.e. make sure it exists, process has permission, lets os know a process is using the file, etc...). In this example only 1024 characters from the file will be read into memory.

    0 讨论(0)
  • 2020-12-01 09:54

    This function will let you read last line or (optionally) entire file line-by-line from end, by passing $initial_pos which will tell from where to start reading a file from end (negative integer).

    function file_read_last_line($file, $initial_pos = -1) {
      $fp = is_string($file) ? fopen($file, 'r') : $file;
      $pos = $initial_pos;
      $line = '';
      do {
        fseek($fp, $pos, SEEK_END);
        $char = fgetc($fp);
        if ($char === false) {
          if ($pos === $initial_pos) return false;
          break;
        }
        $pos = $pos - 1;
        if ($char === "\r" || $char === "\n") continue;
        $line = $char . $line;
      } while ($char !== "\n");
      if (is_string($file)) fclose($file);
      return $line;
    }
    

    Then, to read last line:

    $last_line = file_read_last_line('/path/to/file');
    

    To read entire file line-by-line from end:

    $fp = fopen('/path/to/file', 'r');
    $pos = -1;
    while (($line = file_read_last_line($fp, $pos)) !== false) {
      $pos += -(strlen($line) + 1);
      echo 'LINE: ' . $line . "\n";
    }
    fclose($fp);
    
    0 讨论(0)
  • 2020-12-01 09:59

    You're looking for the fseek function. There are working examples of how to read the last line of a file in the comments section there.

    0 讨论(0)
  • 2020-12-01 09:59
    function readlastline() 
    { 
           $fp = @fopen("/dosmnt/LOGFILE.DAT", "r"); 
           $pos = -1; 
           $t = " "; 
           while ($t != "\n") { 
                 fseek($fp, $pos, SEEK_END); 
                 $t = fgetc($fp); 
                 $pos = $pos - 1; 
           } 
           $t = fgets($fp); 
           fclose($fp); 
           return $t; 
    } 
    

    Source: http://forums.devshed.com/php-development-5/php-quick-way-to-read-last-line-156010.html

    0 讨论(0)
  • 2020-12-01 10:00

    this the code of Ionuț G. Stan

    i modified your code a little and made it a function for reuseability

    function read_last_line ($file_path){
    
    
    
    $line = '';
    
    $f = fopen($file_path, 'r');
    $cursor = -1;
    
    fseek($f, $cursor, SEEK_END);
    $char = fgetc($f);
    
    /**
    * Trim trailing newline chars of the file
    */
    while ($char === "\n" || $char === "\r") {
        fseek($f, $cursor--, SEEK_END);
        $char = fgetc($f);
    }
    
    /**
    * Read until the start of file or first newline char
    */
    while ($char !== false && $char !== "\n" && $char !== "\r") {
        /**
         * Prepend the new char
         */
        $line = $char . $line;
        fseek($f, $cursor--, SEEK_END);
        $char = fgetc($f);
    }
    
    return $line;
    }
    

    echo read_last_line('log.txt');

    you will get that last line

    0 讨论(0)
  • 2020-12-01 10:02

    This should work:

    $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 === "\n" || $char === "\r") {
        fseek($f, $cursor--, SEEK_END);
        $char = fgetc($f);
    }
    
    /**
     * Read until the start of file or first newline char
     */
    while ($char !== false && $char !== "\n" && $char !== "\r") {
        /**
         * Prepend the new char
         */
        $line = $char . $line;
        fseek($f, $cursor--, SEEK_END);
        $char = fgetc($f);
    }
    
    echo $line;
    
    0 讨论(0)
提交回复
热议问题