PHP read last few lines of the file without copying the entire file to memory
<p>Well you know I can use this:</p>

<pre><code>&lt;?php
$myfile = 'myfile.txt';
$command = "tac $myfile &gt; /tmp/myfilereversed.txt";
exec($command);
$currentRow = 0;
$numRows = 20;  // stops after this number of rows
$handle = fopen("/tmp/myfilereversed.txt", "r");
while (!feof($handle) &amp;&amp; $currentRow &lt;= $numRows) {
   $currentRow++;
   $buffer = fgets($handle, 4096);
   echo $buffer."&lt;br&gt;";
}
fclose($handle);
?&gt;
</code></pre>

<p>But doesn't it copy the whole file to memory?</p>

<p>A better approach maybe <code>fread()</code> but it uses the bytes so might also not be the a good approach too.</p>

<p>My file can go into around 100MB so I want it.</p>
