Forum Moderators: coopster

Message Too Old, No Replies

setting permisions on dir with PHP

DIR allowed?

         

davejs

12:57 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



Hi

Im writing a php program which writes .php files to a dir on server. To get this to work I have to chmod the DIR im writing files into to 777 ( I have done this with cpanel filemanager)

This then lets me write files. great!

Now.. I want to make the whole process work in PHP without use of filemanager...

This will then allow me to chmod a DIR to 777, write the files...then chmod back to a more safe 744.

I have just found that i can not chmod a DIR in php... is this correct?

If so is there a way around this? Someone suggested i use exec() to exec("chmod -R 777 dir/");

Does this sound like a good idea?

Many thanks to anyone who can point me in the right direction!

Dave

RonPK

4:35 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, the fine world of owners and permissions... I'm no expert, but maybe this helps:

I suppose CPanel creates directories with your username. That makes you, davejs or whatever your username is on that server, the owner of the directory. The owner is the user who has permission to modify permissions, i.e. decide who can do what in the directory.

Your PHP scripts are run by Apache, with a username like (often) httpd. The user httpd does not own your directory, so httpd cannot change its permissions.

All this means that exec() won't help, as it still is httpd who tries to chmod a directory it doesn't own.

Can't you have PHP create the directory?

timster

8:07 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have just found that i can not chmod a DIR in php... is this correct?

No and yes. PHP has a chmod function ([us2.php.net ]). But, as RonPK mentioned, it won't do you much good if it's an Apache-served Web page, because that will run unprivileged.

I'm guessing you want to do this with a PHP Web page (that's a little tricky and has a bunch of sticky security implications). But if PHP from the command line is all you need, that's pretty easy.

davejs

8:12 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



thanks for response..

Yes i could make the php create the DIR.

Would this then mean that the "owner" of the dir would be set to me? which would allow me to make the dir writeable (777) and then write the files...and then chmod (in php) back to 744

My main concern is that the dir is left "safe" from anyone else writing to it.

Does this sound like it should work?

Thanks

davejs

9:45 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



yes i need this to run from a php web page... or i can run it as a cron job if that helps?

sorry my last answer crossed over with yours (timster)..

so you are saying just because i made the DIR from within a php page dosnt mean i will be able to chmod the dir to a different value?

davejs

10:20 am on Aug 11, 2004 (gmt 0)

10+ Year Member



ok..update... It wont even let me create a DIR with :

mkdir("test2", 0777)

all i get is :
Warning: mkdir(test2): Permission denied in ....

Now im really stuffed!

Any ideas?
php is not running in safe mode...
Im getting more and more confused!
Thanks

timster

12:47 pm on Aug 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, using CRON would be a lot easier because you can run a script from CRON with any privileges you choose.

To ease your confusion, an article on Unix file permissions:

[perlfect.com ]