As far as i can see there, the only problem is your appending the file, not writing to it, so its adding to what was already in the file.
Having worked on a IIS server for the most part of my time, i never used flock or chmod, why are you using them here?
you can write to your own dat files...