Laravel 5.4 --> 禁止对具有“公共”可见性的存储中的文件使用 403
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44247272/
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
Laravel 5.4 --> forbidden 403 on files in Storage with 'public' visibility
提问by call_me_Vlad
I have been getting an issue with Laravel and the Storage
class.
我遇到了 Laravel 和Storage
班级的问题。
I have create an upload form for users to control images used as logos in their account. I use Laravel File Storageinspired by Flysystem.
我为用户创建了一个上传表单,用于控制在其帐户中用作徽标的图像。我使用受Flysystem启发的Laravel文件存储。
When I save an image, I proceed as follows :
当我保存图像时,我按以下步骤操作:
// Store the logo in the public filesystem, and define a 'public' visibility
$logo = $request->file('logo')->store('logos/'.$account->id, 'public');
// Save the path in the database
$account->update([
'logo' => $logo
]);
The upload works fine, I can find the uploaded picture under the appropriate file structure : storage\app\public\logos\1\automatic-filename.jpeg
上传工作正常,我可以在适当的文件结构下找到上传的图片: storage\app\public\logos\1\automatic-filename.jpeg
In my view, I retrieve the image url as follows :
在我看来,我按如下方式检索图像 url:
<img src="{{ Storage::disk('public')->url($account->logo) }}" />
Which gives me the correct path :
http://www.example.com/storage/logos/1/automatic-filename.jpeg
这给了我正确的路径:
http://www.example.com/storage/logos/1/automatic-filename.jpeg
But no matter what I try, I get a 403 on the image
但无论我尝试什么,我都会在图像上得到 403
Forbidden You don't have permission to access /storage/logos/1/automatic-filename.jpeg on this server.
Forbidden 您无权访问此服务器上的 /storage/logos/1/automatic-filename.jpeg。
I think I have done everything correctly :
我想我已经做对了一切:
I have created the symbolic link from public/storage to storage/app/public with
php artisan storage:link
uploaded files are saved on the server, with a 755 permission. All concerned folders have the same permissions.
我已经创建了从 public/storage 到 storage/app/public 的符号链接
php artisan storage:link
上传的文件保存在服务器上,权限为755。所有相关文件夹都具有相同的权限。
I really don't know where to look...
真不知道去哪里看。。。
If anyone ever got a similar issue, thanks in advance for your input !
如果有人遇到类似问题,请提前感谢您的意见!
Best regards,
此致,
Vlad
弗拉德
回答by kJamesy
From inside /public
, run:
从里面/public
,运行:
chown -R username:group storage
Of course replace username:group
with the same username:group
combo that you find against any other public file or folder when you run ls -l
whilst in the public
folder.
当然,当您在文件夹中运行时,替换username:group
为username:group
您在任何其他公共文件或文件夹中找到的相同组合。ls -l
public
回答by vguerrero
I'm guessing this 403 issue you were experiencing was having place in a shared hosting. When you create the symlink with php artisan storage:link
it contains the full absolute path to the storage/app/public
folder. This is what probably causes the 403 response by the server.
我猜您遇到的这个 403 问题出现在共享主机中。当您使用php artisan storage:link
它创建符号链接时,它包含storage/app/public
文件夹的完整绝对路径。这可能是导致服务器 403 响应的原因。
The solution is create a symlink with a relative path from the project root like
解决方案是使用来自项目根目录的相对路径创建一个符号链接,例如
ln -s ../storage/app/public public/storage
This should solve the permissions problem.
这应该可以解决权限问题。
回答by call_me_Vlad
To all that are interested in my solution...
对于所有对我的解决方案感兴趣的人......
I found out that Laravel was not allowed to access the files because /storage/ is beforethe root of my domain, which points to /public !
我发现 Laravel 不允许访问这些文件,因为 /storage/在我域的根目录之前,它指向 /public !
I changed the config/filesystem.php public root path drive to something public :
我将 config/filesystem.php 公共根路径驱动器更改为 public :
Original config :
原始配置:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Modified config :
修改后的配置:
'public' => [
'driver' => 'local',
'root' => public_path('uploads'),
'url' => env('APP_URL').'/uploads',
'visibility' => 'public',
],
Now all routes related to storage are in the public folder... and I can still use the convinient Storage class !
现在所有与存储相关的路由都在公共文件夹中......我仍然可以使用方便的存储类!
Hope it helps.
希望能帮助到你。
Vlad
弗拉德
回答by Mehmet Hano?lu
I created php artisan storage:link
with rootpermission. So /public/storage/... directory not accessible for users.
我是php artisan storage:link
用root权限创建的。所以 /public/storage/... 用户无法访问目录。
Go to /public
folder with command ls -l
you'll see group:owner permissions.
/public
使用命令转到文件夹,ls -l
您将看到 group:owner 权限。
chown -h storage yourgroup:yourusername
chown -h storage yourgroup:yourusername
Now directory accessible from browser.
现在可以从浏览器访问目录。
回答by J.C. Gras
To access the storage folder you must have a symbolic link in the public folder. So, from public folder type ls
to view the storage link. If don't exist or it's in red, you have to create/updated it.
要访问存储文件夹,您必须在公共文件夹中有一个符号链接。所以,从公用文件夹类型ls
来查看存储链接。如果不存在或显示为红色,则必须创建/更新它。
Creating a symbolic link
创建符号链接
From public folder type something like this ln -s /path/to/your/storage/public/ storage
从公用文件夹键入类似这样的内容 ln -s /path/to/your/storage/public/ storage
Fixing a symbolic link
修复符号链接
Find where link pointed to: readlink -v storage
找到链接指向的位置: readlink -v storage
*Point link to new path: ln -sfn /path/to/your/storage/public/ storage
*指向新路径的链接: ln -sfn /path/to/your/storage/public/ storage
*Reference: REPAIRING BROKEN SYMBOLIC LINK
*参考:修复损坏的符号链接
回答by Adam
as kJamesysaid you should check if you have set the wrong owner to your files (for example if you created php artisan storage:link
with root).
正如kJamesy所说,您应该检查是否为文件设置了错误的所有者(例如,如果您php artisan storage:link
使用 root创建)。
To fix this, run from inside /public
:
要解决此问题,请从内部运行/public
:
chown -R username:group storage
Also you could check for file permission (when I copy the files with rsync the often lose read rights for other
leading to 403 Error). If the permissions are not correct, you can fix it by calling from insider /public:
您也可以检查文件权限(当我使用 rsync 复制文件时,经常会因为other
导致 403 错误而失去读取权限)。如果权限不正确,您可以通过从 Insider /public 调用来修复它:
find * -type d -exec chmod 755 {} \;
find * -type f -exec chmod 644 {} \;
回答by emamie
Go to root of laravel project
转到 Laravel 项目的根目录
- Run this command : php artisan storage:link
- Edit public/.htaccess and add to it : Options +FollowSymlinks
- Run this command : chown -h USER:GROUP public/storage
- 运行此命令:php artisan storage:link
- 编辑 public/.htaccess 并添加到其中:Options +FollowSymlinks
- 运行此命令:chown -h USER:GROUP public/storage
Replace USER:GROUPwith correct name.
用正确的名称替换USER:GROUP。