I do it like this, but I'm not sure your directory is set to 777 - that's the only reason I can see for the perms error.
You don't HAVE to change the file perms to 777, the default is (AFAIK) 755, which gives the user Nobody/Nogroup right to read/write/exe the file.
Which works just splendid
if (!file_exists("the_file.txt")) {
touch("the_file.txt"); // Create blank file
chmod("the_file.txt",0777);
}
$a = fopen("the_file.txt", "w");
fputs($a, $string_to_file);
fclose($a);
knutm