title: How to truncate a file at the beginning in PHP?
body: <p>I have a simple logger class written in PHP. The file it writes to should have a maximum size. I know I can truncate a file with <code>ftruncate($filename, $size)</code>, but this removes content at the end. So what I'd like to do is the following:</p>
  
  <pre><code>if(filesize($filename) &gt; $mySize) {
    //remove X bytes at BEGINNING of $filename
  }
  
  // append log message
  $handle = fopen($filename, 'a+');
  if($handle !== false) {
    fwrite($handle, $myMessage);
    fclose($handle);
  }
  </code></pre>
  
  <p>So that the log "scrolls" through the file over time, instead of growing ad infinitum (and in the end crashing at 2GB, the <code>signed int</code> limit of PHP).</p>
  
  <p>What is the best approach to remove beginnings of a file without having to worry to much about performance? I assume reading a file line by line into another cache file is slow, especially if the logging happens rather often. Is there any better way?</p>