13

I currently have a php script that is running when a browser browser browses to the webpage. What I'm trying to do is write a text file when the script runs that stores a variable. The owner of the folder is apache, but everyone has read write, strictly for testing purposes. (I thought it might be a permissions issue) SELINUX is enabled on the server, and when I run the script from console it creates the text file just fine, and in the right directory.

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

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

choppyfireballs
  • 179
  • 1
  • 2
  • 9
  • 1
    your apache user should have rights to directory - just try to execute the script as apache user from the command line to see if that works – user1914292 Feb 15 '13 at 18:12
  • 2
    Check your apache error log, it may give you some details as to where it failed. – Matt Clark Feb 15 '13 at 18:12
  • what is $myString value ? is that correct ? – Ali Akbar Azizi Feb 15 '13 at 18:12
  • What is the error message you get? – Jocelyn Feb 15 '13 at 18:13
  • I'm running it from browser, and it's not flagging any errors, or I should say more accurately I don't know where to get the error messages in browser. @user1914292 I have tried multiple locations including the webroot with apache is the owner of, so I'm not sure if that's necessarily the issue. @CooPer `$myString = strReplace(of an xml string), so it's just a string value – choppyfireballs Feb 15 '13 at 18:17
  • I was mistaken my webroot was not owned by apache so I made a folder and added the apache permissions and ownership thank you @user1914292 – choppyfireballs Feb 15 '13 at 18:30
  • If your having problems on Windows I posted a useful answer here: https://stackoverflow.com/questions/4577149/php-file-put-contents-function-not-working/45785044#45785044 – user2288580 Aug 20 '17 at 17:38

5 Answers5

15

Your problem is likely due to apache not having permissions to write to the file location you specified. Go to that directory and check the permissions and group ownership with the ls command:

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 don't have permissions for apache to write to the directory.

If this is the case, then you will see an error appear in your apache log when it tries to create the file. Try tailing your logs while running the script in your browser:

tail -f /var/log/apache2/error.log
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
2

I had the same trouble recently and stumbled upon this question. Unfortunately choppyfireballs the OP said in a comment he found his own solution and just accepted an answer that wasn't helping any of us... Then after a search and a success to make file_put_contents work again I decided to share my solution.

The permissions of my files and directories were ok to accept any writing (make sure your directories are chmod 757 this will give the root and others the grant to write files in the location). If it still doesn't work like it didn't for me, that's because your system is probably SELinux (Security Enhanced Linux) system.

If you want to make sure write setenforce 0 this will turn selinux to permissive mode, run your script again, if it works then it means the problem is well described.

In that case turn selinux on back setenforce 1 and try ls -Zl in the directory where the directory of your project is. this will give you a line like

drwx---r-x.  9  root   root  system_u:object_r:httpd_sys_content_t:s0  4096  Dec  8  00:25  project

or something different but httpd_sys_content_t if you used chcon to transfer the context from one directory to this one. but if you don't have httpd_sys_content_t it's ok because we need to change the context of that directory anyways.

first you need to accept any public_content_rw_t contexts to write file. Type

setsebool -P httpd_anon_write on

This will set (P)ermanently SELinux boolean httpd_anon_write to true and any context dubbed as public_content_rw_t will have the rights to write any files in their own location.

Now you have to say SELinux that your project directory is public_content_rw_t or you'll still not be able to write files. Type :

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.

vdegenne
  • 12,272
  • 14
  • 80
  • 106
  • Why is SELinux doing this? It seems stupid as it's a FPM process in my case, entirely separate from the website writing to this, heavily guarded, no user input for path. I've had to add 777 / 667 to the folder which is worse than allowing an attacker to write the folder. I honestly feel like these people must have been huffing paints when they designed SELinux, it's no more secure, it just makes utterly brain-damaged decisions about everything for no good reason – MrMesees May 22 '19 at 18:44
0

I ran into this problem too. In my case, I found that the ownership of the directory was wrong. For a typical Apache installation the directory should be owned by www-data:www-data, not root:root.

Bob Reite
  • 1
  • 1
0

Something else to try, for people with a similar question. You might just be making a simple mistake that doesn't require you to mess around with the file permissions—and if you're making this mistake, fixing the file permissions might not help.

Be sure you're using a local, relative file path in file_put_contents().

For example, use:

file_put_contents('short_local_path/my_working_file.txt', $myString);

Not:

file_put_contents('http://example.com/remote_path/my_working_file.txt', $myString);

And not:

file_put_contents('/whole/root/file/path/to/my_working_file.txt', $myString);
Lorien Brune
  • 830
  • 10
  • 16
-3

Have you tried chmodding the directory to 777?

Try this:

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

Might of misspelled something. ^

Xhynk
  • 13,513
  • 8
  • 32
  • 69
Vlad
  • 795
  • 1
  • 12
  • 35