I think the deal is that you need correct permissions for the PHP process, not for you, and the PHP process is not the owner of the directory. So you probably need at least group write permission (665 or 765) but probably 666 or 766. Try 665 first, then 666. If you have to grant public write to write the file, what I have done is surround the file write line with chmods and change the perms, write the file, then take away public write. Not the best, but it's better than nothing.
Here's the deal. The "user" trying to write the files is your webserver. If the webserver is run as "nobody" then "nobody" would need to have rights to write files whereever your script calls for them.