3

I am trying to create a folder and then another folder within it using PHP.

If this is the directory structure I have

/home/site                           (owner : user1)

Now, I create the folder using

mkdir("/home/site/newdir",0777);     (user : apache)

The directory /home/site/newdir is created but the user of that directory is "apache"

Now, doing

mkdir("/home/site/newdir/anotherdir",0777);

doesnt create another directory inside newdir.

Please help. I think its a owner issue. I cannot change the owner using chmod() either. The owner remains the same.

What might be causing this ?

EDIT :

<?php
error_reporting(E_ALL);

mkdir("./m",0777);  // works
mkdir("./m/v",0777); // doesnt work

And no errors on the page.

var_dump(is_writeable("./m")) // returns bool(true)

EDIT : This has been fixed. For others who might be facing the same issue, It was because of PHP's safe mode being "on". Still dont know the reason behind what exactly does safe mode do that doesnt let you create nested directories.

But it works now. Thanks all for reading.

7
  • 1
    You are creating the directory with 0777 permissions, this must not be a permissions problem. Does PHP display any error when trying to create a directory inside /home/site/newdir ? Commented Sep 10, 2011 at 16:07
  • No errors. var_dump(is_writeable("./m")) returns true. Check my edited post. Commented Sep 10, 2011 at 16:14
  • 2
    What about mkdir("./m/v", 0777, true);? Commented Sep 10, 2011 at 16:23
  • Fixed. Please check original post. Thanks all for helping out. Commented Sep 10, 2011 at 16:28
  • Note: chmod can never change the owner of a file or directory, that's not what it does. chown does that, but under any reasonable server setup, you won't be able to actually use it (you need to be root). Commented Sep 12, 2011 at 2:32

1 Answer 1

1

The mode on the directory created by mkdir() is affected by your current umask, which is why chmod() is not working for you.

Try:

$old_mask = umask(0);
mkdir("/home/site/newdir/anotherdir",0777);
umask($old_mask);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.