【问题标题】:Unable to display an image when another is displayed from the same directory从同一目录显示另一个图像时无法显示图像
【发布时间】:2018-10-29 13:00:41
【问题描述】:

我在同一目录中有两个页面home.blade.phppost.blade.php,即:resources/views 目录。当我在主页中显示图像时:

<img src="storage\file_uploads\{{$item->id}}">

它运行良好并显示图像。但是当我在帖子页面中做同样的事情时:

<img src="storage\file_uploads\{{$post->id}}">
<!-- It is needed to mention that the $post and $item variable are not causing the problem for sure and the file does exist-->

我已经运行了一个 php artisan 命令:php artisan storage:link 来链接公共文件夹和存储文件夹,一切都很顺利。 那么,可能是什么问题。

【问题讨论】:

  • 到底是什么问题?为什么在 Web 上下文中使用反斜杠?
  • 已经解决了!

标签: php html laravel image


【解决方案1】:

随着您的 URL 根据您的路由而变化,HTML 标签中定义的路径也会因为它们是相对的而发生变化。

要解决这个问题,请使用如下图像标签:

<img src="{{ asset('storage/file_uploads')}}/{{$post->id}}" >

注意: CSS 和 JS(样式和脚本)的其他导入标记也以类似方式导入。像这样:

<link rel="stylesheet" href="{{ URL::asset('yourpath/css/style.css') }}">
<script src="{{ URL::asset('yourpath/js/app.js') }}"></script>

【讨论】:

    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 2016-04-11
    • 2013-05-12
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多