Jump to content

Recommended Posts

Posted

Hi,

 

I'm trying to write a PHP script to upload image files via an HTML form. So far it all works, but I have to CHMOD the destination directory to 777. It appears that 755 is a standard, more secure value for a directory, so I figured I'd CHMOD the directory to 777 before the upload and back o 755 afterwards. Here's the bit of code in question:

>chmod($upload_dir, 0777);

Unfortunately, I get this error message:

Warning: chmod(): Operation not permitted in /home/domain/public_html/resamp.php on line 12

(the above path was altered to protect the innocent)

 

Can anyone spot what I'm doing wrong? Is this a server setting by TCH that prevents me from CHMODing directories via PHP? I have been successful when CHMODing files within a directory using the same script, so it seems like there must be a solution. Yet, it eludes me.

Posted

Users are only allowed to CHMOD files or directories that are owned by that user. This is not due to a config setting in PHP or the web server - it is a security feature of the operating system.

 

PHP scripts run under the user ID of the web server, which is 'nobody'. This means that PHP scripts can only CHMOD files and directories that are owned by the user 'nobody'. In your case, $upload_dir is owned by you (your user ID), so your PHP script is not allowed to change that directory's permissions with CHMOD.

 

Your script is able to CHMOD files because those files are owned by the user 'nobody'. I'm guessing that your script is changing the permissions of files that were uploaded by the script. Since the script is creating those files on the server, they are owned by the user running the script - 'nobody', and with the script running as 'nobody', it is allowed to change their file permissions.

 

I think your best bet is to set the directory permissions to 0777 yourself and leave them there, since your script isn't able to change them anyway. :)

 

Hope this helps...

Posted

David,

 

Thanks for the reply. Your answer explains quite a bit about the various problems I was having when developing this script.

PHP scripts run under the user ID of the web server, which is 'nobody'.  This means that PHP scripts can only CHMOD files and directories that are owned by the user 'nobody'.  In your case, $upload_dir is owned by you (your user ID), so your PHP script is not allowed to change that directory's permissions with CHMOD.

Yes, I can see that the files uploaded by my script are owned by 'nobody' and other files and directories I've uploaded via FTP are owned by my user ID. So now I'm wondering if I can use PHP to make the directory in the first place so that 'nobody' will be the owner. I made one quick attempt at this solution, but encountered another error message. Is this another security feature or should I be able to execute the following:

 

>mkdir($upload_dir, 0777);

Thanks again. As usual, the TCH forum has been an awesome resource.

Posted

You're probably not able to execute that command because in order to create a directory, the user ID that the script is running under has to have write permissions in the parent directory, and 1) that directory is owned by you, and 2) it does not have 0777 permissions.

 

If you CHMOD the parent directory of $upload_dir to 0777, then you should be able to use your PHP script to create $upload_dir, which would be owned by 'nobody' and have 0777 permissions. Once your script has created the directory, you can reset the permissions on the parent directory back to what they were (0755?).

Posted

Further experimentation resulted in exactly what you just said. Thank you for the quick and detailed explanation. I think I'll stick with your first best bet an simply chmod the upload directory to 0777 and leave it that way.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...