Create writable directories

For the project, I am implementing a file upload system. For each user account, I would like the script to create a different subfolder. Let's say their user_id.

Each time a user is added, the system will create a new subfolder for its own downloads. For instance:

Download / - user1 - user2 - user3

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

However, my application cannot write to this folder. How do I configure php to directories with the required file permissions? I tried using chmod without success.

+4
source share
2 answers

This may 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, the mode is ignored on Windows. and 0777 by default. and the third parameter is recursive, which allows you to create subdirectories specified in the path.

+1
source

sometimes a directory created with a different mode than indicated (0755 instead of 0777, etc.). to solve this problem:

 <?php $old = umask(0); mkdir($dir,0777); umask($old); ?> 
0
source

Source: https://habr.com/ru/post/1432327/


All Articles