Learn fwrite, fopen, fclose. You have to have an already-made txt file like file.txt. I forget which file you have to CHmod to 777 though. fopen = opens file; fclose = closes file; and fwrite = writes to file.
$message = put here what you want to write into the file.txt (or if you use the script you posted you can just not define message anymore cause you already used that as $var !
It really only needs 766 or 666, but as its a text file, 777 works fine. 766 would be read/write/execute for owner, read/write for group, and read/write for others.