The function file_put_contents can be used to do this in a simpler way. According to the PHP documentation, this function is the same thing as calling fopen, fwrite and fclose functions:
This Function is identical to Calling fopen(), fwrite() and fclose() successively to write data to a file.
That is, this function serves as a simplified way to write data in a file.
$msg = 'teste' . PHP_EOL;
file_put_contents('lista.txt', $msg, FILE_APPEND);
The constant FILE_APPEND
serves to add a new content without deleting the existing one. Therefore, if the function is executed again with a different value, the value "teste"
will not be erased.
PS: Remembering that constant PHP_EOL
serves to add a line break (independent of the operating system).
See the possible ways to open a file and what are the differences between them.
– Woss
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero