Reputation: 12847
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
Upvotes: 3
Views: 16657
Reputation: 1
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
It works for me Make sure change your user accordingly in this case it is deploybot
Upvotes: 0
Reputation: 12847
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
Upvotes: 3
Reputation: 413
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).
Upvotes: 1
Reputation: 5791
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.
Upvotes: 4
Reputation: 7113
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!
Upvotes: 1
Reputation: 163898
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
Upvotes: 2