Laravel从存储中检索图像以进行查看 [英] Laravel Retrieve Images from storage to view
问题描述
我正在使用以下代码存储上传的文件
I am using below code to store the uploaded file
$file = $request->file($file_attachment);
$rules = [];
$rules[$file_attachment] = 'required|mimes:jpeg|max:500';
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()
->with('uploadErrors', $validator->errors());
}
$userid = session()->get('user')->id;
$destinationPath = config('app.filesDestinationPath') . '/' . $userid . '/';
$uploaded = Storage::put($destinationPath . $file_attachment . '.' . $file->getClientOriginalExtension(), file_get_contents($file->getRealPath()));
上传的文件存储在 storage/app/2/filename.jpg
我想向用户显示他上传的文件.我该怎么办?
I want to show back the user the file he uploaded. How can i do that?
$ storage =存储::: get('/2/filename.jpg');
$storage = Storage::get('/2/filename.jpg');
我收到不可读的文本.我可以确认该文件已被读取.但是如何将其作为图像显示给用户.
I am getting unreadable texts. I can confirm that the file is read. But how to show it as an image to the user.
希望我的观点清楚.
工作解决方案
display.blade.php
display.blade.php
<img src="{{ URL::asset('storage/photo.jpg') }}" />
web.php
Route::group(['middleware' => ['web']], function () {
Route::get('storage/{filename}', function ($filename) {
$userid = session()->get('user')->id;
return Storage::get($userid . '/' . $filename);
});
});
感谢:@Boghani Chirag和@rkj
Thanks to: @Boghani Chirag and @rkj
推荐答案
无法像您所说的那样公开访问文件,然后像这样读取文件
File not publicly accessible like you said then read file like this
$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg');
假设您的文件位于路径storage/app/{$userid}/file.jpg
且默认磁盘为local
选中config/filesystems.php
Assuming your file is at path storage/app/{$userid}/file.jpg
and default disk is local
check config/filesystems.php
可公开访问的文件
File publicly accessible
如果要使文件可公开访问,则将文件存储在此storage/app/public
文件夹中.您可以在其中创建子文件夹并将其上传到该文件夹.将文件存储在storage/app/public
中后,您只需创建一个符号链接,laravel就会使用artisan命令.
If you want to make your file publicly accessible then store file inside this storage/app/public
folder. You can create subfolders inside it and upload there. Once you store file inside storage/app/public
then you have to just create a symbolic link and laravel has artisan command for it.
php artisan storage:link
这将创建storage/app/public
到public/storage
的符号链接.意味着现在您可以像这样访问文件
This create a symbolic link of storage/app/public
to public/storage
. Means now you can access your file like this
$contents = Storage::disk('public')->get('file.jpg');
此处文件物理路径位于storage/app/public/file.jpg
,并且通过符号链接路径public/storage/file.jpg
here the file physical path is at storage/app/public/file.jpg
and it access through symbolic link path public/storage/file.jpg
假设您有子文件夹 storage/app/public/uploads
,您可以在其中存储上传的文件,然后可以像这样访问它
Suppose you have subfolder storage/app/public/uploads
where you store your uploaded files then you can access it like this
$contents = Storage::disk('public')->get('uploads/file.jpg');
当您在公共文件夹中上传时,您可以在视图中访问它
When you make your upload in public folder then you can access it in view
echo asset('storage/file.jpg'); //without subfolder uploads
echo asset('storage/uploads/file.jpg');
检查详细信息 https://laravel.com/docs/5.6/filesystem#configuration
这篇关于Laravel从存储中检索图像以进行查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!