setting file permissions for fopen, frwrite, etc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

setting file permissions for fopen, frwrite, etc

Post by phpBever »

I'm having trouble figuring out how to set the file permissions for the file I want to write to from my php code. The only thing I seem to be able to make work is 777, which seems too permissive.

I don't see any "php user." There's an apache demon user called wwwrun. I made this user part of the user group, but 775 did not work. There are bits and pieces of answers here and there but they don't add up to a clear general statement that helps me.

So if my /srv/www directory is owned by me, DOCUMENT_ROOT is /srv/www/htdocs, the group is 'users', and the directory has 775 permissions, and I want to do this:

Code: Select all

$file =  fopen("$DOCUMENT_ROOT/../orders.txt", "a");
to create and write to a file, what user besides myself and wwwrun needs to be in the 'users' group? Or is this the wrong way to think about it?

Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: setting file permissions for fopen, frwrite, etc

Post by Christopher »

You probably want these files to be the user/group of wwwrun. That is what PHP runs as inside the web server. Probably you should add yourself as a member of the wwwrun group.
(#10850)
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: setting file permissions for fopen, frwrite, etc

Post by phpBever »

Good--thanks. That works. Why is that different from having wwwrun in the user group with me as owner and having permissions set to 775? (At least I think that was the setup I tried that didn't work.)
Post Reply