Sorry, but I have to ask this. Do you have permissions to write in the location you are trying to create a file? Your AGI script will be executed by the same user as the Asterisk process. Can that user write to that directory? Can you run the script from the command line with success?
Possibly try writing the file some place like /tmp where anyone can write the file.