user1012181
user1012181

Reputation: 8726

mkdir(): Permission denied Laravel

I'm running the following script for an image upload in a server and getting the following error while it works perfectly on the localhost.

Code

$user_id = Auth::id();
$logicpath = 'userdp/' . $user_id . '/';
$pubpath = 'userdp/' . $user_id . '/' . $dpFile;
$path = '/userdp/' . $user_id . '/' . $dpFile;

if (!file_exists($logicpath)) {
     mkdir($logicpath, 0777, true);
}

Error

ErrorException in UploadController.php line 605: mkdir(): Permission denied

at HandleExceptions->handleError('2', 'mkdir(): Permission denied', '/var/www/html/laravel/app/Http/Controllers/UploadController.php', '605', array('dp' => object(UploadedFile), 'ext' => 'jpg', 'img' => object(Image), 'mime' => 'image/jpeg', 'width' => '200', 'height' => '200', 'fileSize' => '17152', 'dpFile' => 'f12f298ab18d58a59c4ed8a589cd1cdc.jpg', 'user_id' => '1', 'logicpath' => 'userdp/1/', 'pubpath' => 'userdp/1/f12f298ab18d58a59c4ed8a589cd1cdc.jpg', 'path' => '/userdp/1/f12f298ab18d58a59c4ed8a589cd1cdc.jpg'))

I tried chmod 777 public and restarted the server. But it didn't work.

Upvotes: 9

Views: 67426

Answers (8)

Niyaz
Niyaz

Reputation: 947

To solve this problem, go into your laravel project, make your public directory writable.

sudo chmod -R 755 public

Upvotes: 0

Basant Mandal
Basant Mandal

Reputation: 106

Please dont change mode to 777 as it may cause your some serious security issues. Rather try using these commands for changing permission of the folder.

sudo chown -R www-data:www-data /var/www
sudo chown -R $USER:$USER /var/www
chmod -R 755 </userdp> Your upload folder path

Try uploading - It will work - In case it fails try

$user_id = Auth::id();
$logicpath = '/userdp/' . $user_id . '/';
$pubpath = 'userdp/' . $user_id . '/' . $dpFile;
$path = '/userdp/' . $user_id . '/' . $dpFile;

if (!file_exists($logicpath)) {
     mkdir($logicpath, 0755, true);
}

Upvotes: 0

Vasu Naik
Vasu Naik

Reputation: 191

ERROR:

  [ErrorException]            
  mkdir(): Permission denied 

If you are creating Project in /WWW folder and if you are encountering the above error, then try using following commands, It worked for me.

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

sudo chown -R $USER:$USER /var/www

After Running this, Try Creating Project again.

cd /var/www

composer create-project laravel/laravel projectname "5.6.*"

since i'm using laravel 5.6 i have specified version while creating Project.

Upvotes: 6

Good Muyis
Good Muyis

Reputation: 147

Running chmod -R 777 may not work and should never be used for security reasons, this has always been a warning.

=> This will work of for you as stated above by @bokino12

sudo chown -R www-data:www-data /var/www/yoursite/public

=> if for some reason it doesn't work, that could be that the present user doesn't have permission over the directory, try

sudo chown -R $USER:$USER /var/www/yoursite/public

This will assign the current user as the owner of the directory.

Upvotes: 2

Binoy Sarker
Binoy Sarker

Reputation: 121

I have found a very interesting solution to this problem. Just put "." sign like this example. It works for me.

$destinationPath = "./public/uploads"; // upload path
    if (!file_exists($destinationPath)) {
      mkdir($destinationPath, 0755, true);
    }
    $request->sub_category_attr_value->move($destinationPath, $picName);

Upvotes: 4

bokino12
bokino12

Reputation: 349

Try:

sudo chown -R www-data:www-data /var/www/yoursite/public

Upvotes: 12

Jocke Med Kniven
Jocke Med Kniven

Reputation: 4049

You are trying to move the uploaded file a folder in the root of your server. Make sure you get the absolute path right.

$logicpath = public_path() . '/userdp/' . $user_id . '/';

Upvotes: 3

Jhonny Montoya
Jhonny Montoya

Reputation: 109

I tried chmod 777 public and restarted the server. But it didn't work.

Be sure you use the CHMOD recursively and with administrator permissions (if you can)

SUDO CHMOD -R 777 /var/www/html/laravel/

Regards

Upvotes: -9

Related Questions