Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir(): Permission denied Laravel

Tags:

php

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.

like image 871
user1012181 Avatar asked Sep 07 '15 16:09

user1012181


People also ask

How to solve mkdir(): permission denied?

[ErrorException] mkdir(): Permission denied. That means you do not have write permission on your project folder. Create a new folder, say 'myproject and run sudo chmod 777 myproject . Then move to 'myproject' folder and create project.

Can not create directory permission denied?

There are a couple of possible solutions to this issue: Create a folder that the user running the build has permissions to. Change the ownership of the directory with the chown command before trying to write to it.


2 Answers

Try:

sudo chown -R www-data:www-data /var/www/yoursite/public
like image 143
bokino12 Avatar answered Sep 28 '22 22:09

bokino12


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);
like image 36
Binoy Sarker Avatar answered Sep 28 '22 21:09

Binoy Sarker