is it possible to read only last line of text file.

size of file is unknown.

    You could possibly use fseek() to start from the end of the file, increment backwards one char at at time until you find a line ending, then read from there to eof.

      Or use [man]file[/man] to read it into an array, and [man]end[/man] to get the last element.

        True, but he wanted to "read only last line".

          • [deleted]

          From the online documentation for fseek:

          Here is a function that returns the last line of a file. This should be quicker than reading the whole file till you get to the last line. If you want to speed it up a bit, you can set the $pos = some number that is just greater than the line length. The files I was dealing with were various lengths, so this worked for me.

          <?php 
          function readlastline($file) 
          { 
                 $fp = @fopen($file, "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; 
          } 
          ?> 
          
            Write a Reply...