It has always baffled me that if a folder has read and write permissions, Apache + PHP can't write to it. You have to end giving apache execution permissions to be able to write a file in that folder. Why?
For example with chmod 664 (where the build group includes the apache user) apache won't be able to write a temporary cache file in this folder:
drw-rw-r-- 5 jenkins build 4096 Jun 15 13:05 cache
while with chmod 774 apache will be able to write a new file to the folder:
drwxrwxr-- 5 jenkins build 4096 Jun 29 11:44 cache