php mkdir(): 权限被拒绝 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32443175/
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-08-25 22:59:08  来源:igfitidea点击:

mkdir(): Permission denied Laravel

phplaravel

提问by user1012181

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'))

UploadController.php 第 605 行中的 ErrorException: mkdir(): Permission denied

在 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', '路径' => '/userdp/1/f12f298ab18d58a59c4ed8a589cd1cdc.jpg'))

I tried chmod 777 publicand restarted the server. But it didn't work.

我尝试chmod 777 public并重新启动了服务器。但它没有用。

采纳答案by Geoherna

You have to perform chmod recursively to make sure all subdirectories also carry the same permissions. Try running this instead:

您必须递归地执行 chmod 以确保所有子目录也具有相同的权限。尝试运行这个:

chmod -R 777 

Now while this will fix your issue, you should definitely read up on the problems that arise with setting the above permissions as this link posted in comments below shows. Ill post here as well:

现在,虽然这将解决您的问题,但您绝对应该仔细阅读设置上述权限时出现的问题,如以下评论中发布的此链接所示。我也在这里发帖:

http://stackoverflow.com/a/11271632/2790481

http://stackoverflow.com/a/11271632/2790481

EDIT: This should only be done within the Laravel storage dir within /var/www/html/laravel/storage, for example and never be done at the system level. I forgot to mentioned that originally, hence all the downvotes.

编辑:这应该只在 Laravel 存储目录中完成/var/www/html/laravel/storage,例如,永远不要在系统级别完成。我最初忘了提到这一点,因此所有反对票。

回答by bokino12

Try:

尝试:

sudo chown -R www-data:www-data /var/www/yoursite/public

回答by user2479930

You are trying to move the uploaded file a folder in the root of your server. Make sure you get the absolute path right.

您正在尝试将上传的文件移动到服务器根目录中的文件夹。确保您获得正确的绝对路径。

$logicpath = public_path() . '/userdp/' . $user_id . '/';

回答by Binoy Sarker

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);

回答by Good Muyis

Running chmod -R 777may not work and should never be used for security reasons, this has always been a warning.

运行chmod -R 777可能不起作用,出于安全原因不应使用,这一直是一个警告。

=> This will work of for you as stated above by @bokino12

=> 正如@bokino12 上面所述,这对你有用

sudo chown -R www-data:www-data /var/www/yoursite/public

sudo chown -R www-data:www-data /var/www/yoursite/public

=> if for some reason it doesn't work, that could be that the present user doesn't have permission over the directory, try

=> 如果由于某种原因它不起作用,那可能是当前用户没有目录权限,请尝试

sudo chown -R $USER:$USER /var/www/yoursite/public

sudo chown -R $USER:$USER /var/www/yoursite/public

This will assign the current user as the owner of the directory.

这会将当前用户指定为目录的所有者。

回答by Jhonny Montoya

I tried chmod 777 public and restarted the server. But it didn't work.

我尝试了 chmod 777 public 并重新启动了服务器。但它没有用。

Be sure you use the CHMOD recursively and with administrator permissions (if you can)

确保您以递归方式使用 CHMOD 并具有管理员权限(如果可以)

SUDO CHMOD -R 777 /var/www/html/laravel/

Regards

问候

回答by zeeawan

Same error for the command

命令的相同错误

composer create-project --prefer-dist laravel/laravel MyProject

But it worked after the following commands

但它在以下命令后工作

sudo mkdir MyProject

sudo chmod -R 777 MyProject

Or better run composer create-project command to some different path than htdocs

或者更好地将 composer create-project 命令运行到与 htdocs 不同的路径