Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com May 16 '10 at 23:57

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 2 down vote accepted

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
share|improve this answer
    
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
2  
This 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). –  Lohoris 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.

share|improve this answer

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!

share|improve this answer
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). –  Lohoris 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.