不允许使用Laravel加载本地资源 [英] Not allowed to load local resource with Laravel
问题描述
我已经在Laravel应用程序中放置了一个上传表单,该文件上传并保存了,但是当我尝试显示它时,我得到了joew:1 Not allowed to load local resource: file:///C:/xampp/htdocs/socialNet/public/uploads/joew.png
I have put in an upload form in my Laravel application, the file uploads and saves it but when I try to display it I get joew:1 Not allowed to load local resource: file:///C:/xampp/htdocs/socialNet/public/uploads/joew.png
这是我放入filesystems.php文件中的根
This is the root I put in the filesystems.php file
'public' => [
'driver' => 'local',
'root' => storage_path('public/uploads'),
'visibility' => 'public',
],
因此,照片被保存在存储文件夹中名为public的文件夹中.
So the photo is being saved inside a folder called public inside the storage folder.
这是检索文件的功能
public function getUserAvatar()
{
if ($this->username) {
$userAvatar = User::where('username', $this->username)->value('avatar');
return "{$userAvatar}";
}
if (!$user) {
abort(404);
}
}
这是它保存在数据库中的格式.
and this is the format that it is saved in the database.
C:\xampp\htdocs\socialNet\public\uploads\joew.png
推荐答案
不允许您的浏览器直接从硬盘驱动器加载该文件.在您看来,您可能会得到这样的信息:
Your browser is not allowed to directly load that file from your harddrive. In your view you are probably getting something like this:
<img src="C:\xampp\htdocs\socialNet\public\uploads\joew.png" />
您需要一个URL,浏览器才能访问该图像.在您看来,您需要以下内容:
You need a URL for the browser to access this image. In your view you need something like:
<img src="{{ asset('uploads/joew.png') }}" />
Laravel的asset()
函数从public
文件夹创建URL,并附加您提供的参数.因此,这将创建一个类似以下的URL:
http://localhost/public/uploads/joew.png
The asset()
function from Laravel creates a URL from the public
folder and appends the param you give it. So this will create a URL like:
http://localhost/public/uploads/joew.png
您指定的文件夹(storage_path)可能根本无法被浏览器读取.将上载更改为public_path('uploads')
,它将可以访问.
The folder you specified (storage_path) is probably not readable for the browser at all. Change the uploads to public_path('uploads')
and it will be accessible.
这篇关于不允许使用Laravel加载本地资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!