title: Need to write at beginning of file with PHP
body: <p>I'm making this program and I'm trying to find out how to write data to the beginning of a file rather than the end. "a"/append only writes to the end, how can I make it write to the beginning? Because "r+" does it but overwrites the previous data.</p>
  
  <pre><code>$datab = fopen('database.txt', "r+");
  </code></pre>
  
  <p>Here is my whole file:</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Facebook v0.1&lt;/title&gt;
          &lt;style type="text/css"&gt;
              #bod{
                  margin:0 auto;
                  width:800px;
                  border:solid 2px black;
              }
          &lt;/style&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;div id="bod"&gt;
              &lt;?php
                  $fname = $_REQUEST['fname'];
                  $lname = $_REQUEST['lname'];
                  $comment = $_REQUEST['comment'];
                  $datab = $_REQUEST['datab'];
                  $gfile = $_REQUEST['gfile'];
  
                  print &lt;&lt;&lt;form
                  &lt;table border="2" style="margin:0 auto;"&gt;
                      &lt;td&gt;
                          &lt;form method="post"  action=""&gt;
                                First Name :
                                &lt;input type ="text"
                                           name="fname"
                                           value=""&gt;
                                &lt;br&gt;
  
                                Last Name :
                                  &lt;input type ="text"
                                           name="lname"
                                           value=""&gt;
                                &lt;br&gt;
  
                                Comment :
                                &lt;input type ="text"
                                           name="comment"
                                           value=""&gt;
                                &lt;br&gt;
                                &lt;input type ="submit" value="Submit"&gt;
                          &lt;/form&gt;
                      &lt;/td&gt;
                  &lt;/table&gt;
                  form;
                  if((!empty($fname)) &amp;&amp; (!empty($lname)) &amp;&amp; (!empty($comment))){
                      $form = &lt;&lt;&lt;come
  
                      &lt;table border='2' width='300px' style="margin:0 auto;"&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;span style="color:blue; font-weight:bold;"&gt;
                                  $fname $lname :
                                  &lt;/span&gt;
  
                                  $comment
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                      come;
  
                      $datab = fopen('database.txt', "r+");
                      fputs($datab, $form);
                      fclose($datab);
  
                  }else if((empty($fname)) &amp;&amp; (empty($lname)) &amp;&amp; (empty($comment))){
                      print" please input data";
                  } // end table
  
                  $datab = fopen('database.txt', "r");
  
                  while (!feof($datab)){
                      $gfile = fgets($datab);
                      print "$gfile";
                  }// end of while
              ?&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>