devinemike did a great job with that code but there are two things missing:
1) You need to close the file with an fclose()
2) Whenever you create a file on disk that may be accessed many times from multilpe browser instances, you need to check for file sharing... (i.e. flock/funlock). If you don't, you will corrupt the file.'
Better to be safe than sorry....
Look at the below for an example:
http://us3.php.net/flock