Laravel 从存储中检索图像以查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50997652/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:50:10  来源:igfitidea点击:

Laravel Retrieve Images from storage to view

laravel

提问by Alaksandar Jesus Gene

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()));

The uploaded files are stored in storage/app/2/filename.jpg

上传的文件存放在storage/app/2/filename.jpg

I want to show back the user the file he uploaded. How can i do that?

我想向用户展示他上传的文件。我怎样才能做到这一点?

$storage = 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.

我收到无法阅读的文本。我可以确认文件已被读取。但是如何将其作为图像显示给用户。

Hope i made my point clear.

希望我说清楚了。

Working Solution

工作解决方案

display.blade.php

display.blade.php

<img src="{{ URL::asset('storage/photo.jpg') }}" />

web.php

网页.php

Route::group(['middleware' => ['web']], function () {
    Route::get('storage/{filename}', function ($filename) {
        $userid = session()->get('user')->id;
        return Storage::get($userid . '/' . $filename);
    });
});

Thanks to: @Boghani Chirag and @rkj

感谢:@Boghani Chirag 和 @rkj

采纳答案by rkj

File not publicly accessible like you said then read file like this

文件不能像您说的那样公开访问,然后像这样读取文件

$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg'); 

Assuming your file is at path storage/app/{$userid}/file.jpgand default disk is localcheck config/filesystems.php

假设您的文件在路径中storage/app/{$userid}/file.jpg并且默认磁盘是local检查config/filesystems.php

File publicly accessible

文件可公开访问

If you want to make your file publicly accessible then store file inside this storage/app/publicfolder. You can create subfolders inside it and upload there. Once you store file inside storage/app/publicthen you have to just create a symbolic link and laravel has artisan command for it.

如果您想让您的文件可公开访问,请将文件存储在此storage/app/public文件夹中。您可以在其中创建子文件夹并在那里上传。将文件存储在其中 storage/app/public后,您只需创建一个符号链接,laravel 就会为其提供 artisan 命令。

php artisan storage:link

This create a symbolic link of storage/app/publicto public/storage. Means now you can access your file like this

这将创建一个符号链接storage/app/publicto public/storage。意味着现在您可以像这样访问您的文件

$contents = Storage::disk('public')->get('file.jpg'); 

here the file physical path is at storage/app/public/file.jpgand it access through symbolic link path public/storage/file.jpg

这里是文件物理路径storage/app/public/file.jpg,它通过符号链接路径访问public/storage/file.jpg

Suppose you have subfolderstorage/app/public/uploadswhere you store your uploaded files then you can access it like this

假设您有storage/app/public/uploads存储上传文件的子文件夹,那么您可以像这样访问它

$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');

check for details https://laravel.com/docs/5.6/filesystem#configuration

检查详细信息https://laravel.com/docs/5.6/filesystem#configuration

回答by Boghani Chirag

Can you please try this code

你能试试这个代码吗

routes.php

路由文件

Route::group(['middleware' => ['web']], function() {
    Route::get('storage/storage_inner_folder_fullpath/{filename}', function ($filename) {
        return Image::make(storage_path() . '/storage_inner_folder_fullpath/' . $filename)->response();
    });
});

view file code

查看文件代码

<img src="{{ URL::asset('storage/storage_inner_folder_fullpath/'.$filename) }}" />

Thanks

谢谢

回答by Gautam Patadiya

Try this:

尝试这个:

Storage::disk('your_disk_name')->getDriver()->getAdapter()->applyPathPrefix('your_file_name');

Good Luck !

祝你好运 !

回答by Rohit Jadhav

can You please try this

你能试试这个吗

$storage = Storage::get(['type_column_name']);