I have a php script that writes to a file. But when I try to actually write to the file I get permission denied. How can I tell what user name I need to add to the file permission in order for the php to write to it?
3 Answers
How can I tell what user name I need to add to the file permission in order for the php to write to it?
ps axu|grep apache|grep -v grep

- 411
- 5
- 20
-
I gives me `www-data`... So I just add this user to the group or something liek that? Is that a good solution, or does PHP provides me with something better? – thedp May 16 '10 at 21:46
-
2This is completely unrelated to PHP, this is a general server issue (and would belong to serverfault.com). It may be solved in many different ways: giving the file to the user www-data is the most straightforward one, though I can't know if it's the best one for you (unless you give us much more detail). – o0'. May 16 '10 at 21:53
-
There aren't any more details... I guess I'll go with the straightforward approach. – thedp May 16 '10 at 22:12
whatever the file is, or directory for that matter, if apache needs to write to it, it needs to be owned by apache
, httpd
, www-data
, or whatever the user apache is running under on your server. you said in a response that it is www-data
, so as root, you should do chown www-data filename
to change ownership.
i would strongly recommend against changing the permissions to 777, simply because having world readable/writable files and directories on your server can pose some security risks.
ultimately, i would configure your script to write to a set path, then change the ownership of that path to www-data
so future files can be created there if need be, without your interaction being required.
this poses it's own risks, too, as a compromised script can then write and potentially execute whatever it wants from that directory.
as much of a pain as it may be, doing it file by file is less risky.

- 2,720
- 18
- 12
I tend to just CHMOD the file to 777, assuming you're running on a Unix platform. May not be the most secure thing ever, must gets the job done reliably. I'm sure others will have a better solution though!

- 19
- 1
-
1-1 not only this might be insecure, but it is not an actual answer to his question either (though I admit it could be considered some sort of workaround). – o0'. May 16 '10 at 21:37
-
I gave you a +1 to even you up to a zero, for attempting to help. Your solution is insecure and since this is no longer one of my old high school projects I can't accept it. :) – thedp May 16 '10 at 21:42