How to give PHP/Apache write access to folders
Setting write permissions for the Apache web server
To grant the Apache web server write permissions for the "tmp" sub-directory, execute
setfacl -m u:apache:rwx tmp setfacl -m d:u:apache:rwx tmp
The first line sets the write permissions on the folder, the second line creates a "default" entry to ensure that the new rights will be inherited down the tree.
Setting write permissions on existing files
Permissions can be inherited only by sub-folders and files created after the permissions were set on the parent folder. Consequently, the files that existed in "tmp" before the write permissions were set on "tmp" do not have them. To give them the permissions, use the same commands as above, but add option "-R", which secures that the command will be executed not only on the folder itself, but also on all of its sub-folders and files:
setfacl -R -m u:apache:rwx tmp setfacl -R -m d:u:apache:rwx tmp
Now the Apache web server - and PHP - have write access to the "tmp" folder and can modify all files in this folder, even those that were created before the permissions were set.
Attention: Never set Apache write permissions on directories that contain your scripts as this would enable the script to modifiy itself. Always keep your data and programs in separate directories.