You get that if APPPATH.'tmp/' does not exist or is not accessable.
I assume you haven't configured any base area in your file.php config file? Is this on Windows? Does that trailing slash need to be there (I don't think so)?
If the console works, but the code doesn't, chances are you have a rigths issue, where you, as commandline user, are allowed to create it, but the webserver hasn't.
You can see that in my console example above: the file is created with my personal user and group, and not with the (in my case) g_www group, which gives the webserver access to the file.