php file_put_contents function not working

89,916

Solution 1

It could be a permission problem

Is the directory /files chmoded to 777? sometimes php won't allow you to access directories if they don't have enough permission. I don't know about blank errors though.

Try to see if it has enough permissions, if not, then set it to 777 and try it.

Solution 2

Are you using the full path on the filesystem or are you trying to use the URI? I think this PHP function expects you to give the path as the file is found on the filesystem.

Relative paths should be okay though.

You may need to make sure the file exists and that it's permissions are set to 777. Sometimes I've found that it's not enough to just have the directory permissions set to 777 but the file must also already exist.

Solution 3

It is because of SELINUX.

You can mark the folder as of type httpd_sys_rw_content_t with this command to enable writing to this folder.

semanage fcontext -a -t httpd_sys_rw_content_t '/files(/.*)?'; restorecon -RF '/files/'

Solution 4

We've experienced this, requiring a workaround (regardless of method, permissions and anything else mentioned here). When all other fixes failed, we have found it can be linked to restrictions created by SELinux.

Share:
89,916
steve
Author by

steve

Updated on July 09, 2022

Comments

  • steve
    steve almost 2 years

    Why would file_put_contents refuse to work for the following code?

    $f = file_put_contents("files/r.js", "Some text here");
    
    if ($f) print 1;
    else print 0;