Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not entirely sure of your question - do you want to write data and not have it over-write the beginning of an existing file, or write new data to the start of an existing file, keeping the existing content after it?</p> <p><strong>To insert text without over-writing the beginning of the file</strong>, you'll have to open it for appending (<a href="http://ie.php.net/fopen" rel="noreferrer"><code>a+</code> rather than <code>r+</code></a>)</p> <pre><code>$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!"); if ($_POST["lastname"] &lt;&gt; "") { fwrite($file,$_POST["lastname"]."\n"); } fclose($file); </code></pre> <p><strong>If you're trying to write to the start of the file</strong>, you'll have to read in the file contents (see <a href="http://www.google.ie/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fwww.php.net%2Ffile_get_contents&amp;ei=39jTSKOWEoyk1wbQ-vC6Dg&amp;usg=AFQjCNHwFDwvy4v0p90sNfmfSB_jg7gR7Q&amp;sig2=KyOdq6fGxuj8701pDDdXqA" rel="noreferrer"><code>file_get_contents</code></a>) first, then write your new string followed by file contents to the output file.</p> <pre><code>$old_content = file_get_contents($file); fwrite($file, $new_content."\n".$old_content); </code></pre> <p>The above approach will work with small files, but you may run into memory limits trying to read a large file in using <code>file_get_conents</code>. In this case, consider using <a href="http://ie.php.net/rewind" rel="noreferrer"><code>rewind($file)</code></a>, which sets the file position indicator for handle to the beginning of the file stream. Note when using <code>rewind()</code>, not to open the file with the <code>a</code> (or <code>a+</code>) options, as:</p> <blockquote> <p>If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position. </p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload