Laravel 5 - 如何访问在视图中上传到存储中的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30191330/
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 - How to access image uploaded in storage within View?
提问by Tadeá? Jílek
I have got user's avatars uploaded in Laravel storage. How can I access them and render them in a view?
我已经在 Laravel 存储中上传了用户的头像。如何访问它们并在视图中呈现它们?
The server is pointing all requests to /public
, so how can I show them if they are in the /storage
folder?
服务器将所有请求指向/public
,如果它们在/storage
文件夹中,我如何显示它们?
回答by Bogdan
The bestapproach is to create a symbolic linklike @SlateEntropy very well pointed out in the answer below. To help with this, since version 5.3, Laravel includes a commandwhich makes this incredibly easy to do:
在最好的方法是创建一个符号链接像@SlateEntropy相当不错,指出了下面的答案。为了帮助解决这个问题,从 5.3 版开始,Laravel包含了一个命令,使这变得非常容易:
php artisan storage:link
That creates a symlink from public/storage
to storage/app/public
for you and that's all there is to it. Now any file in /storage/app/public
can be accessed via a link like:
从创建一个符号链接public/storage
到storage/app/public
你,这一切就是这么简单。现在/storage/app/public
可以通过以下链接访问任何文件:
http://somedomain.com/storage/image.jpg
If, for any reason, your can't create symbolic links (maybe you're on shared hosting, etc.) or you want to protect some files behind some access control logic, there is the alternative of having a special route that reads and serves the image. For example a simple closure route like this:
如果出于任何原因,您无法创建符号链接(也许您使用的是共享主机等),或者您想保护某些访问控制逻辑背后的某些文件,则可以选择使用特殊的路由来读取和服务于形象。例如一个简单的关闭路线,如下所示:
Route::get('storage/{filename}', function ($filename)
{
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
You can now access your files just as you would if you had a symlink:
您现在可以像使用符号链接一样访问您的文件:
http://somedomain.com/storage/image.jpg
If you're using the Intervention Image Libraryyou can use its built in response
method to make things more succinct:
如果您正在使用干预图像库,您可以使用其内置response
方法使事情更简洁:
Route::get('storage/{filename}', function ($filename)
{
return Image::make(storage_path('public/' . $filename))->response();
});
WARNING
Keep in mind that by manually servingthe files you're incurring a performance penalty, because you're going through the entire Laravel request lifecycle in order to read and send the file contents, which is considerably slowerthan having the HTTP server handle it.
警告
请记住,手动提供文件会导致性能下降,因为您要经历整个 Laravel 请求生命周期才能读取和发送文件内容,这比让 HTTP 服务器处理它要慢得多。
回答by SlateEntropy
One option would be to create a symbolic link between a subfolder in your storage directory and public directory.
一种选择是在存储目录和公共目录中的子文件夹之间创建符号链接。
For example
例如
ln -s /path/to/laravel/storage/avatars /path/to/laravel/public/avatars
This is also the method used by Envoyer, a deployment manager built by Taylor Otwell, the developer of Laravel.
回答by Piotr
According to Laravel 5.2 docs, your publicly accessible files should be put in directory
根据 Laravel 5.2 文档,您的可公开访问的文件应放在目录中
storage/app/public
To make them accessible from the web, you should create a symbolic link from public/storage
to storage/app/public
.
要使它们可从 Web 访问,您应该创建一个从public/storage
到的符号链接storage/app/public
。
ln -s /path/to/laravel/storage/app/public /path/to/laravel/public/storage
Now you can create in your view an URL to the files using the asset helper:
现在,您可以使用资产助手在视图中创建文件的 URL:
echo asset('storage/file.txt');
回答by cabs
If you are on windows you can run this command on cmd:
如果您使用的是 Windows,则可以在 cmd 上运行此命令:
mklink /j /path/to/laravel/public/avatars /path/to/laravel/storage/avatars
from: http://www.sevenforums.com/tutorials/278262-mklink-create-use-links-windows.html
来自:http: //www.sevenforums.com/tutorials/278262-mklink-create-use-links-windows.html
回答by Haider Ali
First of all you need to create a symbolic link for the storage directory using the artisan command
首先,您需要使用 artisan 命令为存储目录创建一个符号链接
php artisan storage:link
Then in any view you can access your image through url helper like this.
然后在任何视图中,您都可以通过这样的 url helper 访问您的图像。
url('storage/avatars/image.png');
回答by Arash Moosapour
If you want to load a small number of Privateimages You can encode the images to base64 and echo them into <img src="{{$image_data}}">
directly:
如果你想加载少量的私有图像,你可以将图像编码为base64并<img src="{{$image_data}}">
直接将它们echo成:
$path = image.png
$full_path = Storage::path($path);
$base64 = base64_encode(Storage::get($path));
$image_data = 'data:'.mime_content_type($full_path) . ';base64,' . $base64;
I mentioned private because you should only use these methods if you do not want to store images publicly accessible through url ,instead you Must always use the standard way (link storage/public folder and serve images with HTTP server).
我提到私有是因为如果您不想存储可通过 url 公开访问的图像,您应该只使用这些方法,而必须始终使用标准方式(链接存储/公共文件夹并使用 HTTP 服务器提供图像)。
Bewareencoding to base64()
have two important down sides:
当心编码base64()
有两个重要的缺点:
- This will increase image size by ~30%.
- You combine all of the images sizes in one request, instead of loading them in parallel, this should not be a problem for some small thumbnails but for many images avoid using this method.
- 这将使图像大小增加约 30%。
- 您在一个请求中组合所有图像大小,而不是并行加载它们,这对于一些小缩略图应该不是问题,但对于许多图像避免使用这种方法。
回答by Syed Shibli
It is good to save all the private images and docs in storage directory then you will have full control over file ether you can allow certain type of user to access the file or restrict.
最好将所有私人图像和文档保存在存储目录中,这样您就可以完全控制文件以太,您可以允许某些类型的用户访问该文件或限制该文件。
Make a route/docs and point to any controller method:
创建一个路由/文档并指向任何控制器方法:
public function docs() {
//custom logic
//check if user is logged in or user have permission to download this file etc
return response()->download(
storage_path('app/users/documents/4YPa0bl0L01ey2jO2CTVzlfuBcrNyHE2TV8xakPk.png'),
'filename.png',
['Content-Type' => 'image/png']
);
}
When you will hit localhost:8000/docs
file will be downloaded if any exists.
当您点击localhost:8000/docs
文件时,将下载文件(如果存在)。
The file must be in root/storage/app/users/documents
directory according to above code, this was tested on Laravel 5.4
.
root/storage/app/users/documents
根据上面的代码,文件必须在目录中,这是在Laravel 5.4
.
回答by dagogodboss
If you are using php then just please use the php symlink function, like following:
如果您使用的是 php,那么请使用 php 符号链接函数,如下所示:
symlink('/home/username/projectname/storage/app/public', '/home/username/public_html/storage')
symlink('/home/username/projectname/storage/app/public', '/home/username/public_html/storage')
change the username and project name to the right names.
将用户名和项目名称更改为正确的名称。
回答by Jehad Ahmad Jaghoub
without site name
没有站点名称
{{Storage::url($photoLink)}}
if you want to add site name to it example to append on api JSON felids
如果您想将站点名称添加到它的示例中以附加到 api JSON felids
public function getPhotoFullLinkAttribute()
{
return env('APP_URL', false).Storage::url($this->attributes['avatar']) ;
}
回答by Dnyaneshwar Harer
If disk 'local' is not working for you then try this :
如果磁盘“本地”不适合您,请尝试以下操作:
- Change local to public in
'default' => env('FILESYSTEM_DRIVER', 'public'),
fromproject_folder/config/filesystem.php
- Clear config cache
php artisan config:clear
- Then create sym link
php artisan storage:link
- 将本地更改为公共
'default' => env('FILESYSTEM_DRIVER', 'public'),
从project_folder/config/filesystem.php
- 清除配置缓存
php artisan config:clear
- 然后创建符号链接
php artisan storage:link
To get url of uploaded image you can use this Storage::url('iamge_name.jpg');
要获取上传图片的网址,您可以使用它 Storage::url('iamge_name.jpg');