Take the 2-minute tour ×

For a project I'm implementing a file-upload system. For every user account I would like the script to create a different sub-folder. Lets say their user_id's.

Each time a user is added, the system will create a new sub-folder for their own uploads. For example:

Uploads/ - user1 - user2 - user3

By executing mkdir('Uploads/'.$user_id, 0777); it will create a new subfolder. Everything is fine.

However my application is not able to write to this folder. How do I have php make directories with the required file permissions? I have tried using chmod with no success.

share|improve this question
On the server, set umask=022? even 000? –  Moe Tsao Sep 4 '12 at 16:48
I hear umask(0) is used here, but I'm not sure. –  Waleed Khan Sep 4 '12 at 16:48
What mode are the folders being created with? And is this 'application' of yours also running from the same webserver, or something external/non-php? –  Marc B Sep 4 '12 at 16:49
The application is running on the same webserver. I have tried umask(0) with no avail. –  Gillian Lo Wong Sep 4 '12 at 16:58
add comment

2 Answers

This might help chmod and mkdir

$dirMode = 0777;
mkdir($directory, $dirMode, true);

// chmod the directory since it doesn't seem to work on recursive paths
chmod($directory, $dirMode);

For mkdir, mode is ignored on Windows. and 0777 is by default. and the third param is recursive which allows the creation of nested directories specified in the pathname.

share|improve this answer
Thanks for your effort but it did not work =( –  Gillian Lo Wong Sep 4 '12 at 17:03
add comment

sometimes the directory created with another mode than specified ( 0755 instead 0777 etc). to solve that use :

$old = umask(0); 
share|improve this answer
Thanks, but this did not seem to work. –  Gillian Lo Wong Sep 4 '12 at 17:03
add comment

Your Answer


