Append input to bottom of textfile

Hi,

I am struggling with appending input from a form to the bottom of a textfile, instead it is added on top of the file.

Any suggestions?

[php]

<?php $question = $_POST['input_question']; $answer = stripslashes(nl2br($_POST['input_answer'])); // check if news input field is empty if (empty ($answer)) { die ("No answer entered.

« Back");} $txt = "index_faq.txt"; $read = fopen("$txt", "r") or die("can't open file"); $content = fread($read, filesize($txt)); fclose ($read); //write into textfile $write = fopen("$txt", "w"); if($write){ flock($write,2); fputs ($write, " • $question
Answer: $answer


$content"); flock($write,3); fclose ($write);} echo ""; ?>

[/php]

fopen — Opens file or URL
[php]resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )[/php]

The mode parameter specifies the type of access you require to the stream. It may be any of the following:

‘w’ Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

‘a’ Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

http://php.net/manual/en/function.fopen.php

when changing

[php]$write = fopen("$txt", “w”);[/php]
to
[php]$write = fopen("$txt", “a”);[/php]
or even a+

it copies the entire textfile content and then appends at the bottom…

Why is that?

Sponsor our Newsletter | Privacy Policy | Terms of Service