Sounds perplexing. What linux distro are you running on? It sounds symptomatic of an selinux problem. If I recall, you have to grant permissions to directories for users explicitly or something along those lines. Drew has some experience with RedHat selinux.
I'm also a little confused. Are you trying to have it write the file to /tmp directory or in the app workspace?
The other confusing thing is that if it is called from within the app, it should be executed by the user running the web application, which should be www-data. So if the www-data user has write access to that directory, it should be fine. Again, it may be an selinux issue though. You have to tell selinux to allow that user to have write permissions to that directory.