I had the same problem and came across this question. Unfortunately, choppyfireballs OP said in a comment that it found its own solution and simply accepted an answer that didnโt help any of us ... Then, after searching and success, to make file_put_contents work again, I decided to share my solution.
The permissions of my files and directories were fine to accept any entry (make sure your directories are chmod 757 , this will give the root file and other grants to write files to this location). If it still does not work, as if it was not for me, it is because your system is probably SELinux (Security Enhanced Linux).
If you want to record setenforce 0 , this will turn selinux into enable mode, run your script again if it works, then the problem is well described.
In this case, turn selinux to back setenforce 1 and try ls -Zl in the directory where your project directory is located. this will give you a string like
drwx---rx. 9 root root system_u:object_r:httpd_sys_content_t:s0 4096 Dec 8 00:25 project
or something else, but httpd_sys_content_t if you used chcon to transfer context from one directory to this. but if you donโt have httpd_sys_content_t , that's fine, because we still need to change the context of this directory.
first you need to accept any public_content_rw_t contexts to write the file. Type of
setsebool -P httpd_anon_write on
This will set (P) ermanently SELinux boolean httpd_anon_write to true, and any context called public_content_rw_t will have permission to write any files to their own location.
Now you must tell SELinux that your project directory is public_content_rw_t , or you wonโt be able to write files anyway. Type of:
semanage fcontext --add --type public_content_rw_t "/project(/.*)?"
and restorecon -RvF /project to tell selinux to apply the above specifications.
Now your directory is public_content_rw_t, and you should be able to write files.