File_put_contents does not create a txt file

I currently have a php script that runs when the browser browser is browsing a web page. What I'm trying to do is write a text file when the script is executed where the variable is stored. The owner of the folder is apache, but everyone reads the record, strictly for testing purposes. (I thought this might be a permission issue) SELINUX is enabled on the server, and when I run the script from the console, it creates a text file just fine and is in the correct directory.

file_put_contents("My working file location", $myString); 

I use this line to try to write and create a text file, I know that my file location works because I can run it and create it offline, IE running it through the console. The problem is that the variable I'm trying to write is populated through the HTTP Post, and when I run the script through the browser, or when apache runs the script, it does not write and does not create the file. What do I need to do to allow write / change syntax access to get this script to write this text file?

+7
source share
5 answers

Your problem is probably due to the fact that apache does not have write permissions to the file location you specify. Go to this directory and check the permissions and ownership of the group with the ls :

 cd "My working file location" ls -l . 

There are three columns in the output that show the permissions, owner and group for the directory. Most likely they are owned by root and do not have apache permissions to write to the directory.

If so, you will see that an error appears in your Apache log when it tries to create a file. Try trimming your logs when running a script in your browser:

 tail -f /var/log/apache2/error.log 
+9
source

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.

+1
source

I ran into this problem. In my case, I found that the ownership of the catalog was incorrect. For a typical Apache installation, the directory should belong to www-data: www-data, not root: root.

0
source

I am sure if my solution is really clear, but it worked:

 cd /mydir/ setsebool -P allow_httpd_anon_write true 

Best wishes

-one
source

Have you tried using chmodding in the 777 directory?

Try the following:

 if(file_put_contents('file.txt', 'text')){ die('yes'); } else { die('no'); } 

Perhaps something is spelled incorrectly. ^

-2
source

All Articles