So, I have this error:
Warning: fopen(/path/to/test-in.txt) [function.fopen]: failed to open stream: Permission denied
Performing ls
ls
Another reason of this error may be that the directory of file does not exist.
I just add php code before fopen:
if(!file_exists(dirname($file))) mkdir(dirname($file));
This help me out.