Open the file in append mode:
$fp = fopen("formdata.txt", "a") or exit("Unable to open file!");
Or use file_put_contents
with the FILE_APPEND
flag:
file_put_contents("formdata.txt", $name . "," . $email . "\n", FILE_APPEND);
Note:
If you're running this under a Windows environment, you will need to use \r\n
.
I.e.:
file_put_contents("formdata.txt", $name . "," . $email . "\r\n", FILE_APPEND);
The \n
alone is sufficient under a UNIX/Linux platform but not for Windows.
You can also use the cross-platform PHP_EOL
pre-defined constant.
file_put_contents("formdata.txt", $name . "," . $email . PHP_EOL, FILE_APPEND);