如何使用laravel 5.4获取存储在存储目录中的头像URL? [英] How to get avatar url which are stored at storage directory using laravel 5.4?
问题描述
我想获取存储目录中存储的化身的网址,我正在使用laravel 5.4.
I want to get the url of stored avatar inside the storage directory, i am using laravel 5.4.
实际上,我正在使用以下方法上传图片并获取网址.
Actually i am using following method for uploading image and get the url.
上传图片:
<input type="file" class="form-control" value="{{ old('u_avatar') }}" name="u_avatar" />
在控制器中
$UserModel->u_avatar = $request->file('u_avatar')->store('avatars'); // Uploading Files properly in the storage/app/avatars
获取图片
我尝试了以下两种获取图像的方法,但两种方法均不起作用
I have tried following two methods for getting the images, but both are not working
第一种方法
在视图文件中
<img src="{{ asset('storage/app') }}/{{ $User->u_avatar }}" class="img-responsive" /> // Not Working
第二种方法 在控制器中
$User = UserModel::find($id);
//For Image
$path = asset('storage/app/'.$User->u_avatar);// Not working
我想请您指导我我做的正确与否?在此参考该人在说
I would like to request to you kindly guide me i am doing in right way or not? In this reference the person is saying
存储不是公开的.您不能在此处保存文件,然后创建指向它们的Web链接.该线程是关于能够使用laravel拦截对图像的调用,从私有存储中获取图像并将文件流式传输到客户端.
storage is not public. You cannot save files there then create web links to them. This thread is about being able to use laravel to intercept the call for the image, get it from private storage and stream the file to the client.
因此有人可以将文件存储在laravel5.4中的最佳方法是什么,以及如何以正确的方式获取文件.我很感激.谢谢.
So can someone what is the best way to store the files in laravel5.4 and how can get in proper way. I would like to appreciate. Thank You.
推荐答案
storage
目录无法从Web上访问,因此您需要创建符号链接.
storage
directory is not accessible from the web, so you need to create a symlink.
公用磁盘用于将要公开访问的文件.默认情况下,公用磁盘使用本地驱动程序,并将这些文件存储在
storage/app/public
中.为了使它们可以从Web上访问,您应该创建一个从public/storage
到storage/app/public
的符号链接.此约定会将您的公共可访问文件保留在一个目录中,当使用零停机时间部署系统(例如Envoyer)时,可以在各个部署之间轻松共享这些文件.
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in
storage/app/public
. To make them accessible from the web, you should create a symbolic link frompublic/storage
tostorage/app/public
. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
要创建符号链接,可以使用storage:link
Artisan命令:
To create the symbolic link, you may use the storage:link
Artisan command:
php artisan storage:link
当然,一旦存储了文件并创建了符号链接,就可以使用资产助手创建指向这些文件的URL:
Of course, once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:
echo asset('storage/file.txt');
https://laravel.com/docs/5.4/filesystem#the -public-disk
这篇关于如何使用laravel 5.4获取存储在存储目录中的头像URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!