At first blush, I see you are using file handle 1. Maybe it is already in use. you should never hard code the file handle in a UDF. You may want to look at using the FreeFileHandle() function. Doublecheck what $ScriptDir is set too as well.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.