Chmod With Php

liunx

Guest
Hi,<br /><br />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:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->chmod($upload_dir, 0777);<!--c2--></div><!--ec2--><br />Unfortunately, I get this error message:<br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->Warning: chmod(): Operation not permitted in /home/domain/public_html/resamp.php on line 12<!--QuoteEnd--></div><!--QuoteEEnd--><br />(the above path was altered to protect the innocent)<br /><br />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.<!--content-->
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.<br /><br />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.<br /><br />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.<br /><br />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. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /> <br /><br />Hope this helps...<!--content-->
David,<br /><br />Thanks for the reply. Your answer explains quite a bit about the various problems I was having when developing this script.<br /><!--QuoteBegin-TCH-David+Jul 1 2005, 11:35 PM--><div class='quotetop'>QUOTE(TCH-David @ Jul 1 2005, 11:35 PM)</div><div class='quotemain'><!--QuoteEBegin-->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.<br /><div align="right"><a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=138061"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><!--QuoteEnd--></div><!--QuoteEEnd--><br />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:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->mkdir($upload_dir, 0777);<!--c2--></div><!--ec2--><br />Thanks again. As usual, the TCH forum has been an awesome resource.<!--content-->
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 <i>parent</i> directory, and 1) that directory is owned by you, and 2) it does not have 0777 permissions.<br /><br />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?).<!--content-->
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.<!--content-->
 
Top