It looks like to me that it's just a security setting. If you created the file first BUT don't change mode to 777, the file should be blank after the script runs. Also are you running apache as root or admin or just some plain user? I recall that the web directories' owner need to be the same user as the one who runs those script.
If nothing works add in chmod("777") or chmod("766") before the fopen statement and change back about writing the file.
Hope this helps.
K. Tsang CEng MBCS PMP PMI-ACP OCPJEA OCPJP
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Two software engineers solve most of the world's problems in one K&R sized book