laravel 无法写入目录 - Digital Ocean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36733306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:38:20  来源:igfitidea点击:

Unable to write in the Directory - Digital Ocean

phplaravellaravel-5permissionschmod

提问by senty

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

1 无法写入“../public/uploads/imgs”目录

2 无法写入“var/www/laravel/public/uploads/imgs”目录



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

我在 Digital Ocean 上使用 NGINX

采纳答案by senty

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

我用 Digital Ocean 的具体案例解决了我的问题..首先我尝试过......

 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 hereand here

更多信息在这里这里

回答by Mysteryos

Starting with your error messages:

从您的错误消息开始:

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

1.无法写入“../public/uploads/imgs”目录

By default, your nginx is pointing to the publicfolder of your laravel installation. ../public/uploads/imgswill therefore resolve to /var/www/laravel/public/uploads/imgswhich won't work if it doesn't have read permissions to the laravelfolder.

默认情况下,您的 nginx 指向public您的 Laravel 安装文件夹。../public/uploads/imgs因此/var/www/laravel/public/uploads/imgs,如果它没有对该laravel文件夹的读取权限,则将解析为哪个将不起作用。

Normally, nginx should only have read access to your publicfolder.

通常,nginx 应该只对您的public文件夹具有读取权限。

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

2.无法写入“var/www/laravel/public/uploads/imgs”目录

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

路径是相对的。它的绝对路径是/var/www/laravel/public/var/www/laravel/public/uploads/imgs默认情况下不存在的。

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

文档:https: //laravel.com/docs/5.1/helpers#paths

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

警告:不要授予 nginx 对您的public文件夹的写访问权限!如果有人设法上传并覆盖您的index.php.

回答by Alexey Mezenin

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

尝试运行此命令以授予对该upload目录及其所有子目录的写权限:

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

回答by Webeng

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!

首先不要包含其他代码,只是为了看看您是否能够实际创建目录。如果它有效并且在那里,请尝试在那里包含文件。如果这对你有用,请告诉我!

回答by naf4me

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.

请在没有特定许可的情况下创建具有适当权限的目标路径 imgs。请检查以下代码,您可以在其中授予 imgs 文件夹的权限。

$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.

makeDirectory 参数的位置将在此处澄清。

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

仅供参考, $mode 将是 0775, 0777 (认为它不应该给它)。

回答by abhishek sethi

sudo usermod -aG www-data deploybot\

sudo usermod -aG www-data deploybot\

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

须藤 chown -R www-data:www-data /var/www/laravel

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

须藤 chown -R www-data:www-data /var/www/laravel/public/uploads

sudo chmod -R 775 /var/www

须藤 chmod -R 775 /var/www

It works for me Make sure change your user accordingly in this case it is deploybot

它对我有用 确保在这种情况下相应地更改您的用户,它是 deploybot