Does chmod(); always work?

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
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Does chmod(); always work?

Post by kaisellgren »

Hi,

I'm wondering a thing that is maybe a bit more a *nix question. Anyhow, can I rely on chmod()? I need to often make directories and files writable undex *nix and asking user to chmod through FTP is not an option. Is this a bad thing - using a chmod? My application is targeted for LAMP setups.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Does chmod(); always work?

Post by Syntac »

chmod() works fine on *nix systems. Windows support is spotty.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Does chmod(); always work?

Post by Mark Baker »

chmod won't work unless you have privilege to actually change the file permissions
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Does chmod(); always work?

Post by VladSun »

Mark Baker wrote:chmod won't work unless you have privilege to actually change the file permissions
That is - you should either be root, or the owner of the file.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Does chmod(); always work?

Post by kaisellgren »

VladSun wrote:
Mark Baker wrote:chmod won't work unless you have privilege to actually change the file permissions
That is - you should either be root, or the owner of the file.
That is - any files I create in PHP are owned by PHP, which means I can chmod them?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Does chmod(); always work?

Post by VladSun »

kaisellgren wrote:
VladSun wrote:
Mark Baker wrote:chmod won't work unless you have privilege to actually change the file permissions
That is - you should either be root, or the owner of the file.
That is - any files I create in PHP are owned by PHP, which means I can chmod them?
Yes.
Indeed, most probably files are owned by the Apache user.
There are 10 types of people in this world, those who understand binary and those who don't
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Does chmod(); always work?

Post by Mark Baker »

VladSun wrote:Indeed, most probably files are owned by the Apache user.
Should really be by daemon, or by the user specified in apache's httpd.conf.
Of course, daemon need not actually exist as a *nix user on the server.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Does chmod(); always work?

Post by kaisellgren »

I tried running this script in the ROOT folder of three different servers:

Code: Select all

<?php
mkdir('adirectory',0777);
fopen('adirectory/afile.php','w');
chmod('adirectory/afile.php',0777);
?>
And it seemed to work create. One thing, the adirectory is not 0777, its 0755, but that's enough for me.

Are there servers where you can't make php to create files/folders into the root?
Post Reply