Set your open mode to "append" instead of "write" (see the documentation for fopen
):
$fp = fopen('somefile.txt', 'a');
If you are on Windows, you may want to consider using the t
flag to denote the file you are opening as a text file, so that the proper line-ending character is being used (\r\n
instead of just \n
for Windows):
$fp = fopen('somefile.txt', 'at');
Now, to add your text onto new lines, make sure to use PHP_EOL
for the newline character character to ensure that the right newline characters are being written for the right OSs:
fwrite($fp, PHP_EOL . 'Some text');
fwrite($fp, PHP_EOL . 'More text on another line');