title: PHP: Read from certain point in file
body: <p>Similar to: <a href="https://stackoverflow.com/questions/2961618/how-to-read-only-5-last-line-of-the-txt-file/10711998#10711998">How to read only 5 last line of the text file in PHP?</a></p>
  
  <p>I have a large log file and I want to be able to show 100 lines from position X in the file.
  I need to use <code>fseek</code> rather than <code>file()</code> because the log file is too large.</p>
  
  <p>I have a similar function but it will only read from the end of the file. How can it be modified so that a start position can be specified as well? I would also need to start at the end of the file.</p>
  
  <pre><code>function read_line($filename, $lines, $revers = false)
  {
      $offset = -1;
      $i = 0;
      $fp = @fopen($filename, "r");
      while( $lines &amp;&amp; fseek($fp, $offset, SEEK_END) &gt;= 0 ) {
          $c = fgetc($fp);
          if($c == "\n" || $c == "\r"){
              $lines--;
              if($revers){
                  $read[$i] = strrev($read[$i]);
                  $i++;
              }
          }
          if($revers) $read[$i] .= $c;
          else $read .= $c;
          $offset--;
      }
      fclose ($fp);
      if($revers){
          if($read[$i] == "\n" || $read[$i] == "\r")
              array_pop($read);
          else $read[$i] = strrev($read[$i]);
          return implode('',$read);
      }
      return strrev(rtrim($read,"\n\r"));
  }
  </code></pre>
  
  <p>What I'm trying to do is create a web based log viewer that will start from the end of the file and display 100 lines, and when pressing the "Next" button, the next 100 lines preceding it will be shown.</p>