Page 1 of 1

Chmod

Posted: Mon May 29, 2006 3:27 pm
by andym01480
I wanted to use chmod() to change the permission on a config.inc.php after a setup.php script had finished. To make it so it couldn't be written to (0755).

But I can't get it to work!

I'm using PHP4 so ftp_chmod is not available.

I've tried fiddling with the directory permissions and the permission of the file to 777 (albeit just while playing!) and it just doesn't work. Safe mode is off.

Code: Select all

Warning: chmod(): Operation not permitted in /home/#####/public_html/guestbook/chmodtest.php on line 3
I've hashed out my username for security!

Code: Select all

<?php
chmod("config.inc.php",0755);
?>
Any clues on what to try next?

Posted: Mon May 29, 2006 4:22 pm
by andym01480
Worked out that chmod() will only work if I create the file, to do that I need to make its directory 777 before I start. Then I can't use my script to chmod the directory because i don't own it. So am back to square one.

Is this why most PHP apps have ponderous "you must chmod these files to ..." when you ftp rather than doing it from the script itself?

What is the point of chmod()?

Posted: Mon May 29, 2006 5:45 pm
by pickle
You can't change the permissions of that file because apache doesn't own it. When you run PHP scripts, I think generally they run as the "apache" user. If your user owns the files, apache won't be able to touch it without proper permissions.

chmod() changes the "mode" of a file or directory. It sets up the permissions - what the owner/group/world are/is allowed to do to the file/directory.

You're going to need to change the permissions of the file when you FTP it up so that the group or the world can modify it - then apache & your PHP script should be able to chmod() it.