senty senty - 4 months ago 49
PHP Question

Unable to write in the Directory - Digital Ocean

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

Answer

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