title: optimal way to prepend a file in php
body: <p>Prepend a string to a file in php</p>
  <ul>
  <li>Size of file can be very large.</li>
  <li>Need an optimal solution while keeping performance as the key factor.</li>
  </ul>
  <p>This is what i have so far</p>
  <pre><code>    function prepend($string, $filename) { 
          $context = stream_context_create (); 
          $fp = fopen($filename,'r',1,$context); 
          $tmpname = md5($string); 
          file_put_contents($tmpname,$string); 
          file_put_contents($tmpname,$fp,FILE_APPEND); 
          fclose($fp); 
          unlink($filename); 
          rename($tmpname,$filename); 
      } 
  </code></pre>