Forum Moderators: coopster

Message Too Old, No Replies

PHP chmod question

PP chmod question: script occasionally changes directory permissions.

         

webid

10:52 am on May 11, 2009 (gmt 0)

10+ Year Member



Hi everyone,

I run a file hosting site using a script that I purchased on the net a while back. In general, it runs really well and does everything that I need it to except one thing:

The permissions to the folder where uploaded data is stored sometimes changes from 0777 to 0644 causing new uploads to fail until the permissions are chmod back to 0777.

The only file which uses PHP chmod is one called success which handles the uploaded file, inserts info into database, etc. It is used in the following way:
// CHMOD File
@chmod($uploadDir.$file, 0644);

The problem I am having is that sometimes the permissions for the entire folder change, rather than just the file and I have two questions:

1) Is there any apparent reason for this in the code?

2) What are the implications of either deleting the line or changing it to chmod files to something other than 0644, perhaps 0700 or 0755 so that if it does change the folder permissions it will still be writable by the script?

I am a noob with very basic understanding of PHP.

[edited by: coopster at 1:14 pm (utc) on May 11, 2009]
[edit reason] removed code [/edit]

coopster

1:18 pm on May 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, webid.

I removed the code as it was unnecessary, didn't provide any details that pertain to your issue. It seems there may be something running server side that is updating/changing your file permissions at the folder level, not your script. If it is in the script, it may be occurring in one of the included files but highly unlikely. If you ever use FTP or some other tool you may be changing the permissions there as well.

Does your folder ever get removed and recreated?

webid

2:57 pm on May 11, 2009 (gmt 0)

10+ Year Member



Thank you Coopster.

My apologies for the unnecessary code.

The folder doesn't get moved or recreated.

The odd thing is that it works fine most of the time but then it will just randomly reset to 0644. This usually happens within a week, despite the fact that hundreds of files are uploaded everyday.

My host insists that it's not an issue with the dedicated server that I have with them.

Do you think it would be a safe bet to remove the chmod code from the success.php file and see if that has any connection to the problem? Is there any value in specifically chmodding a file to 0644, especially considering that I am on a dedicated server?

penders

3:54 pm on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@chmod($uploadDir.$file, 0644);

What if the value of $file is empty? (Something causes it to be reset, failed upload, untrapped error etc.) Would that cause just $uploadDir (ie. the directory) to get changed to 0644?

webid

4:01 pm on May 11, 2009 (gmt 0)

10+ Year Member



Penders, that is something I considered.

Is it viable to put in some code to say that if the value is empty, do nothing?

This seems even more plausible since it happens quite randomly.

penders

5:26 pm on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it viable to put in some code to say that if the value is empty, do nothing?

Or, if the value is NOT empty then do something...


if (!empty($file)) {
@chmod($uploadDir.$file, 0644);
}

Or, only if the value is in fact a real file (and not simply a directory) then do something...


$newfile = $uploadDir.$file;
if (is_file($newfile)) {
@chmod($newfile, 0644);
}

However, I would have thought that if this is indeed the case then it was better trapping this situation earlier. By the time you are chmod'ing the file it has already been uploaded and the temporary (imaginary?) file has been copied.

webid

4:52 pm on May 12, 2009 (gmt 0)

10+ Year Member



Thanks for your help, Penders.

I did some testing and when $file is empty it definitely changes the directory permissions. I changed the code and now that is working. I am still trying to go through the code to get a better understanding but it seems that whatever happens to cause the value of $file to be empty also causes it to delete the temp file.

penders

6:35 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...it seems that whatever happens to cause the value of $file to be empty also causes it to delete the temp file.

Is it possible that there is no temp file, because no file was successfully uploaded and that $file is empty from the beginning?