Jump to content

Errore while file upload: directory permission problem


Recommended Posts

Hi everybody.

I have a problem with my website.

I need to allow the user to create a project with a wizard set-up, with different pages where he upload 3 different files in a directory named by the project id


More or less:

step 1 - Make the directory ID and put the first file in.

step 2 - Put the second file in the directory ID.

step 3 - Put the third file in the directory ID.


Ok, this doesn't sound too difficult, but some things that seems very strange to me are happening.

At first, only the step 1 was working. It was making the directory and putting the first file, but not the other two.

Now it is not working at all (even if I had not changed nothing or, at least, not consciously)


The source code of the first step is:


if (!mkdir($dir, 0777)) {

echo "Impossible create the directory";


return FALSE; }

echo "Created directory :".$dir."<br>Permissions: ".substr(sprintf('%o', fileperms($dir)), -4)."<br>";



The remarkable thing is that this shows that the permissions are set to 0775! I guess this should be the reason why the upload function returns error while doing move_uploaded_file.


What I don't understand at all is WHY the permissions are set to 0775 if I created the directory with permissions 0777!

Besides, I tried to change the permissions even directly in the upload function (this is needed for the second and third file). Here is the code:



if (!chmod($dir, 0777)) { $e=-3; //if cannot change permissions on the dir, put a -3 on the error code, if any

}else {$e=0;}

echo "<br> Permissions: ".substr(sprintf('%o', fileperms($dir)), -4)."<br>";


if (move_uploaded_file($_FILES[$file]['tmp_name'],$target)) {

//chmod($dir, 0775);

return -127;

} else {

//chmod($dir, 0775);

return -1-$e;


Surprisingly enough, the echo shows again that permissions are at 0775.


Trying to understand the matter, I even tried this code:

if (mkdir($new_dir, 0777)==TRUE) { echo "success";

} else {

echo "error";}

Now, this doesn't work at all (return error)!

(Note that the previous code at least DO create the directory, even if I cannot move files there as the permission is not 0777).


I´m wondering... should I make some change to the parent directory? Can this be the reason? Or can it be a problem with the company who provides the hosting? Or, what mistakes there are in the code?


Any help will be extremely appreciated, thanks....

Link to comment
Share on other sites

Thank for your answer.

Ok, I checked out, and indeed the safe mode is on. :/

Badly I cannot change this... Is there any solution?

I was thinking to move my website to an other hosting (now it is on a free hosting what I use for trying if it works), but I've right now tried with an other free hosting service (with safe_mode off), and it sais: Warning: mkdir() [function.mkdir]: Permission denied in /home/****/prova.php on line 5


I'm going mad with this  ;D

Link to comment
Share on other sites

Ops, my bad, I made a spelling mistake while writing the directory path in the second case (with safe mode off).

Here it create the directory, but not with the permission what I want, and, besides, it works only if I create it inside an other directory with permissions on 0777 (and I wouldn't like too much to leave such a directory on the website)...

Link to comment
Share on other sites

Mh, ok, it seems that the whole issue was because of the safe mode...

Also, I've read that on windows hostings I cannot set the permissions while creating the directory.

Actually my server is not windos based, but anyway by using simply mkdir with no permissions settings, and a chmod after it, the whole thing works.


But there is one problem still. The basic script seems to work, IF the parent directory is on 0777.


Which, I think, is not exactly a good thing.


Even a chmod($root_path, 0777);

is useless, as I got:

Warning: chmod() [function.chmod]: Operation not permitted in /home/*****/public_html/prova.php on line 7


So, I have to find out some way to do this chmod, or I have to lead the parent directory on 777...

Meanwhile, here is the code:



chmod($root_path, 0777); //change permission to the container dir ---- DOESN´T WORK!

$new = $root_path."/try";

echo "<br>Directory to crate: ".$new."<br>";

if (mkdir($new)==TRUE) { echo "success";

} else {

echo "error";}

chmod($new, 0777);

echo "<br>Permissions: ".substr(sprintf('%o', fileperms($new)), -4)."<br>"; //check out if this time the dir has the right permission, 0777

//Here will come the uploads

chmod($new,0775); //Change back the permissions of the directories to avoid security problems

chmod($root_path,0775); //DOES NOT WORK!

So, the directory $new is correctly created, the permissions is correctly set to 0777 to permit the uploads, then it is correctly changed on 0775 to secure the system.

The chmod on $root_path doesn't seem to have any effect (in the end it doesn't show the warning, but I've checked out the directory and it is still as before, so it didn't worked).

Link to comment
Share on other sites

I found out the problem.

I needed to create the parent dir with php. Then php is the owner and I can do whatever.

I was creating the directory with the FTP client, so it wasn't giving to php the right to do what I needed.

Thanks to HuggieBear for his advice. :)

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.