Unable to write in the Directory - Digital Ocean

12,251

Solution 1

I solved my problem with specific case of Digital Ocean.. First I tried...

 useradd -U -G www-data -m -c "deploy" deploybot
 chown deploybot:www-data /var/www/laravel
 chmod 2755 /var/www/laravel

Then..

 sudo usermod -aG www-data deploybot
 sudo chown -R www-data:www-data /var/www/laravel
 sudo chown -R www-data:www-data /var/www/laravel/public/uploads
 sudo chmod -R 775 /var/www

More info here and here

Solution 2

Starting with your error messages:

1. Unable to write in the "../public/uploads/imgs" directory

By default, your nginx is pointing to the public folder of your laravel installation. ../public/uploads/imgs will therefore resolve to /var/www/laravel/public/uploads/imgs which won't work if it doesn't have read permissions to the laravel folder.

Normally, nginx should only have read access to your public folder.

2. Unable to write in the "var/www/laravel/public/uploads/imgs" directory

The path is a relative one. It's absolute path is /var/www/laravel/public/var/www/laravel/public/uploads/imgs which won't exist by default.

Solution:

Replace these lines:

$directory = '../public/uploads/imgs/';
$file->move(base_path().$directory, $fileName);

With:

$directory = 'uploads/imgs/';
$file->move(public_path().$directory, $fileName);

Documentation: https://laravel.com/docs/5.1/helpers#paths

Warning: Do not give nginx write access to your public folder! If someone manages to upload & overwrite your index.php, your site will be compromised.

Solution 3

Try to run this command to give write permissions to the upload directory and all it's subdirectories:

sudo chmod -R 775 /var/www/laravel/public/uploads

Solution 4

Try first creating the directory with:

$directory_path = "...";//whatever you wish it to be

//if the directory doesn't exist already, it is created:
if (!file_exists($directory_path))
{
  mkdir($directory_path);
}

At first don't include the other code, just to see if you are able to actually create a directory. If it works and its there, try including the file there. Let me know if that worked for you!

Solution 5

Please create target path imgs with proper permission rather without specific permission. Please check the following codes where you can give permission to imgs folder.

$destinationPath = public_path().'/uploads/imgs/';

// Create folders if they don't exist
if (!file_exists($destinationPath)) {
   File::makeDirectory($destinationPath, $mode = 0755, true, true);
}

Where makeDirectory params are will clarified at here.

FYI, $mode would be 0775, 0777 ( thought it should not give it).

Share:
12,251
senty
Author by

senty

Harder, Better, Faster, Stronger...

Updated on June 28, 2022

Comments

  • senty
    senty almost 2 years

    I am trying to upload an image with post request and move it to a directory. I tried..

    $file = $request->file('file');
    $extension = strtolower($file->getClientOriginalExtension());
    
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
        
    $generatedName = sha1(time().time());
    $directory = '../public/uploads/imgs/'; // works
    $fileName =  $generatedName . "." . $extension; // works
    
    // 1
    $file->move($directory, $fileName);
    // 2
    $file->move(base_path().$directory, $fileName);
    

    At this point, I am receiving error:

    1 Unable to write in the "../public/uploads/imgs" directory

    2 Unable to write in the "var/www/laravel/public/uploads/imgs" directory


    I thought it was caused by permissions but didn't help either.. I tried

    sudo chmod 770 /var/www/laravel/public/uploads (also /imgs/
    and
    sudo chmod -R 775 /var/www/laravel/public/uploads 
    

    I also found something else to try but couldn't figure out what to write in username bit:

    sudo chown username:www-data /var/www/laravel/public/uploads/
    

    I am using NGINX on Digital Ocean

  • senty
    senty about 8 years
    Still getting the same errors.. This doesn't seem to help for both (1) and (2)
  • senty
    senty about 8 years
    I tried it. I put print("A") inside the if but I forgot to include it in the first time. At the end, I included $file->move($directory, $fileName); however, I started receiving Unable to write in the "../public/uploads/imgs" directory
  • Webeng
    Webeng about 8 years
    ok so if your getting the "Unable to write..." error, then almost certainly its permissions, though if you already changed it to writable as you said in your question, then I can't be sure. Maybe posting an inquiry in the forum of your webhosting provider, or even calling them should work, sadly it is tedious...
  • senty
    senty about 8 years
    Ye, unfortunately sudo chmod -R 775 /var/www/laravel/public/uploads doesn't change anything :(
  • Webeng
    Webeng about 8 years
    @senty I know that in CPanel, when I go into the file manager where I can see all my directories and files, the permissions code is in one of the columns and I can change them manually by clicking on them. That might work for you.
  • senty
    senty about 8 years
    Unfortunately, I don't use CPanel, I use nginx from digital ocean.. What may be the problem causing it? :/
  • Webeng
    Webeng about 8 years
    Since I have never used nginx from digital ocean, I can't be sure. It could be that the settings are somewhere hidden, it could be that your permissions are blocked for some reason (maybe you are using free hosting that doesn't allow it). I can only guess. Your best bet would be to search if other users had similar problems online while using nginx. I hope that helps.
  • Mysteryos
    Mysteryos about 8 years
    Do a ps aux | grep nginx in your console. Look for nginx worker process in the furtherest right column. Confirm if your first column at that line reads www-data
  • senty
    senty about 8 years
    Yes it does. I have 4 of worker process with first column www-data
  • Mysteryos
    Mysteryos about 8 years
    Alright the problem ain't with your server, it's in the code. Be right back writing an answer.
  • senty
    senty about 8 years
    I solved it with the above code that I included in the answer
  • saber tabatabaee yazdi
    saber tabatabaee yazdi over 4 years
    i can do that in my local and also can do that in my server 2012 as laravel host but cant do that with my linux cpanel server... all those 3 codes and laravel same version. i dont know why chmod -R ...../images run as 777 but image never uploaded
  • Mysteryos
    Mysteryos over 4 years
    @sabertabatabaeeyazdi It's best to verify your logs to check where the error is exactly. See: laravel.com/docs/5.1/errors
  • saber tabatabaee yazdi
    saber tabatabaee yazdi over 4 years
    strange think is that in cpanel i put my public folder in api.mydomain.com and other laravel files in root of my cpanel and when call public_http method it returned me /home/mydomain/laravel/images but i need /home/mydomain/api.mydomain.com/images so i catch it and solved