简体   繁体   中英

How to give permission for my Laravel application create subfolders in a directory (Ubuntu)

I have a folder that will be the container for subfolders created by user of my application. I use the following commands to let my application execute the mkdir() function: "sudo chmod -R 775 /var/www/laravel/public/folder_want_give_permission_to", "sudo chmod -R 777 /var/www/laravel/public/folder_want_give_permission_to", "chown -R www-data:www-data folder_want_to_give_permission_to". As I am testing my application, visiting the pages that execute the mkdir function, I have to run the commands from time to time because it appears that Ubuntu "forgets" that I allowed permission in the directory and I get "ErrorException: mkdir(): Permission denied". Things get worse when I remove all subfolders from the directory that I gave permission, the error appears right after I create the first subfolder. The Ubuntu version I am using is 16.04.

To show my mkdir() I think I have to explain the variable values involved.

My system counts the number of subdirectories to manage the user directories to avoid creating directories with the same name. To create a user directory I use the code below.

mkdir( $userdirectory, 0755, true);

$userDirectory would have a value like this, if the user was the first user to register.

"/var/www/laravel/public/folder_want_to_give_permission_to/1"

This would create a subdirectory inside the folder I want to grant permission for them create additional subfolders to store images.

Now to create the subfolders to store images inside the user folder.

mkdir( $userdirectory. $numSubDir, 0755, true);

$folder has a value something like this "/var/www/laravel/public/folder_want_to_give_permission_to/1/" and $numSubdir stores a number like "1" or "2" to store images.

I would end up with something like this: /var/www/laravel/public/folder_want_to_give_permission_to/1/1

So a user could have directories to store images like this:

/var/www/laravel/public/folder_want_to_give_permission_to/1/1
/var/www/laravel/public/folder_want_to_give_permission_to/1/2

another user

/var/www/laravel/public/folder_want_to_give_permission_to/2/1
/var/www/laravel/public/folder_want_to_give_permission_to/2/2

if what I am trying to do isn't clear: /var/www/laravel/public/folder_want_to_give_permission_to/user_directory/folder_to_store_images_1

/var/www/laravel/public/folder_want_to_give_permission_to/user_directory/folder_to_store_images_2

a

Please run the below command, hope this helps.

sudo chown -R www-data:www-data /var/www/laravel/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM