【问题标题】:How to display the user image from storage/app?如何显示存储/应用程序中的用户图像?
【发布时间】:2019-03-24 23:50:45
【问题描述】:

如何显示存储文件夹中的图像(外部公共)?我将图像文件存储在 storage/usernamee 中,但无法在视图中显示。如何将上传文件移至“storage/images/”(在文件夹名称“images”下)?

查看

<form class="forms-sample" method="post" action="/updateprofil" enctype="multipart/form-data">
    @method('put')
    @csrf
    <div align="center">
        <img alt="User Pic" src="{{  storage_path($membre->photo) }}" id="profile-image1"
             class="img-circle img-responsive">
        <input id="pic" class="hidden" name="pic" type="file" accept="image/*">
        <div style="color:#999;">click here to change profile image</div>
    </div>
    <input type="hidden" id="idMembre" name="membre_id" value="{{  $membre->id }}">
</form>

控制器

public function updateprofil(Request $request)
{
    $request->validate(['cin' => 'size:8']);

    if ($request->hasFile('pic')) {
        $image = $request->file('pic');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        Image::make($image)->resize(300, 300)->save(storage_path("app/usernamee/" . $filename));
        $request->pic = $filename;
    }

    $membre = Membre::findOrFail($request->membre_id);
    $membre->photo = 'app/usernamee/' . $request->pic;
    $membre->update($request->all());

    return redirect('profil');
}

This is my view of user profile

PS:update 方法适用于信息更新,但不适用于照片。

【问题讨论】:

    标签: laravel laravel-storage


    【解决方案1】:

    您需要或需要:

    1) 需要将图像保存在storage/app/public 目录中 - 首先您需要将存储路径符号链接到公共:

    php artisan storage:link

    那么你的方法应该可行。

    2) 创建返回图片的路由。

    Route::get('show-image/{id}', function() {
       $membre = Membre::findOrFail($id);
    
       $file = storage_path($membre->photo);
    
       return response()->file($file);
    })
    

    【讨论】:

    • 我在本地存储上工作,为什么我需要在公共目录中?
    • 如果你想公开展示图片,使用#1,否则使用#2
    • 我用#2 完成了它,它可以工作,但是当我编辑一个字段(例如“名称”)时,照片消失并返回为空。请问我该如何解决? (抱歉迟到了)
    • 你的意思是改变文件名吗?
    • 是的,当我更改任何文件时,例如“名字”、“姓氏”、“电子邮件”……我的意思是当我更改其中一个文件时,图像就会消失
    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2020-07-14
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多