Not really related to original issue, but I would suggest just building up a string, then doing one fwrite call at the end. That will be more more efficient and will reduce the number of file writes quite a bit.
Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.
These out-of-the-box solutions provide you proven, tested applications that can be up and running now. Build a store, a gallery, or a web-based email solution.