View Full Version : chmod()?

06-07-2005, 10:24 AM
How can I change permission of a directory?
chmod() can change permission of a file only?
I wanna use a PHP script and change permission of a directory and then write a new (non-existing) file in it. Please help.

06-07-2005, 12:00 PM
I had some problems myself setting file and folder permissions with chmod so I used the following solution and it works for me - if you can chmod via ftp it should hopefully work for you, too:

$target = '/path/from/root/to/file/or/directory/';
$chmod_setting = '0777'; // the permission to set (don't miss the leading 0)

if(!$ftp_connect = @ftp_connect(ftp_server)) {
echo 'Could not connect to ftp server';

if (!@ftp_login($ftp_connect, ftp_user, ftp_pass)) {
echo = 'Could not login with username and password';

$chmod_cmd = "CHMOD " . $chmod_setting . " " . $target;

if(!$chmod = @ftp_site($ftp_connect, $chmod_cmd)) {
echo 'The file/folder permissions could not be changed';

} else {
echo 'New file permissions set';

06-07-2005, 12:10 PM
So the php chmod command can change file modes, while the ftp_site can echo ftp commands like fput and fget, so chmod on the server can change directory modes?

It's kind of funny you have to let the server talk to himself with ftp. :rolleyes:

06-07-2005, 12:25 PM
I'm not entirely sure but I think that chmod() can be limited in the php.ini. On my host I can't use chmod at all, either to change file or directory permissions, but I can use php to login as an ftp user and do exactly the same thing without restriction, weird huh :rolleyes:

06-07-2005, 01:58 PM
Very surprising, PHP has no function to change directory functions :rolleyes:
We use delinear's code so.

06-07-2005, 02:29 PM
Oh! I'm getting error: The file/folder permissions could not be changed
Why this error and only this?

I echoed $target, it is: /home/jewelry/public_html/test.html

So the file isn't created :confused:

06-07-2005, 03:05 PM
Are you perhaps running in safe_mode? Check your php.ini settings. Safe_mode is affecting the way how chmod works. From the manual:

Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits

And of course you can change permissions on directories too via chmod(). At least on a *NIX console you can, I don't see any reason why PHP wouldn't be able to do so. The error stems from a mismatch between the FTP user's permissions and the file owner's permissions. Double-check that the FTP user has the write-permission enabled for the file in question.

06-07-2005, 03:10 PM
phpinfo() shows:

safe_mode Off Off
safe_mode_exec_dir no value no value
safe_mode_gid Off Off

06-07-2005, 03:12 PM
Does the file actually already exist or are you trying to create it? I got a bit confused by your previous post.

06-08-2005, 08:30 AM
Actually, I'm writing a CMS, so I need create/edit/delete files. Is it unsafe, if I give write permission to complete public_html directory? Also, can you suggest a friendly, light-weight yet robust cms ( unlike Mambo, Nuke; these are heavy ).

06-08-2005, 09:44 AM
OK, I've given write permission to public_html and it's all fine now. Another problem poped: I'm using FCKEditor for CMS and it's converting all my PHP code to HTML!! Please help me.

06-08-2005, 10:02 AM
Regarding one of your last questions: Yes, it's unsafe to give write permissions to everyone (owner, group, others) in your whole public_html directory. Try to limit permissions as much as possible. Depends a little on your actual server/hosting setup though, sometimes one needs to chmod a file to 0777.

Those last questions of you have nothing to do with chmod, so I suggest you open seperate threads for them.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum