Spiralio Spiralio - 3 years ago 173
PHP Question

Text does not get appended to file

The text file seems to be overwritten whenever I try appending more text to it. Here's the code:

<?php
header('Location: http://optifine.net/');
$txt = "data.txt";
$fh = fopen($txt, 'w+');
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$txt=$_POST['field1'].' - '.$_POST['field2'];
file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt
exit();
}
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file

?>

Answer Source

OP says "text file seems to be overwritten" means contents are written to file, so this is not an issue related to permissions.

try opening your file with

$fh = fopen($txt, 'a+');

instead of your line saying;

$fh = fopen($txt, 'w+');

Refer to PHP Documentation at http://php.net/manual/en/function.fopen.php

Explanations about mode.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download