【问题标题】:How to display image that is stored in public folder?如何显示存储在公用文件夹中的图像?
【发布时间】:2018-08-22 14:43:21
【问题描述】:

我在控制器中使用以下存储功能。我上传的图像存储在 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');
}

【问题讨论】:

  • 您可能想查看您的示例:括号中存在不匹配。我把它标记得更整齐了,所以问题更清楚了。
  • 嘿,我明白了,谢谢:)
  • 您通常将文件存储在storage 目录下的storage_path('folder') 路径下。在您的情况下,storage_path('public/images') 会很棒。然后,当您使用 php artisan storage:link 时,将为公共可用路径 /public 创建指向私有路径 /storage/public 的符号链接。这也增加了更多的安全性,因为公共路径通常没有写权限。
  • image}}" height="100" width="100">

标签: php laravel-5 laravel-blade


【解决方案1】:

这是您认为的方法。

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2011-11-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2017-02-28
    相关资源
    最近更新 更多