PHP file created with fopen disappears

In my script I use fopen to create a txt file. When executing the script I can see the txt file being created on the ftp but when I refresh the ftp contents the file is gone? Is this normal behaviour or am I not using the fopen command correctly?

I am using it like this:

$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');

Answer

Solution:

If you are wanting to continuously write to this file (i.e. append more contents to it, since it is an error log), try using this:

$errorMessage = 'Test writing to errorlog.txt';
file_put_contents(dirname(__FILE__).'/errorlog.txt', $errorMessage, FILE_APPEND | LOCK_EX);

Source