如何显示存储在公用文件夹中的图像?

时间:2018-07-28 13:11:09

标签: php laravel-5 laravel-blade

我在控制器中使用以下存储功能。即时上传的图片将存储在public / images /文件夹中。如何在视图中显示该图像?请帮助我。

public function store(Request $request) {
    $this - > validate($request, [
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $imageName = time().'.'.$request - > image - > getClientOriginalExtension();
    $request - > image - > move(public_path('images'), $imageName);

    $stock = Stock::create([
        'tag_no' => $request - > input('tag_no'),
        'image' => $imageName,
        'user_id' => Auth::user() - > id
    ]);

    if ($stock) {
        return redirect()->route('stocks.index', ['stocks' => $stock - > tag_no])->with('success', 'Stock created successfully');
    }
}
return back() - > withInput() - > with('errors', 'Error creating new Stock');
}

1 个答案:

答案 0 :(得分:0)

这是您认为的方法。

<img src="/images/{{$stock->image}}" height="100" width="100">

如果您已将图像存储在自定义文件夹中,例如:

 $request->image->move(public_path('/storage/'.Auth::user()->company.'/stock-images'), $imageName);

此处的文件夹为 / public / storage / companyname / stock-images /

因此要显示,请使用以下代码:

<a href="/storage/{{Auth::user()->company}}/stock-images/{{$stock->image}}" target="__blank">
<img class="pull-right" src="/storage/{{Auth::user()->company}}/stock-images/{{$stock->image}}"  height="100" width="100"></a>