Chmod

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
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Chmod

Post 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?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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()?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply