douxu4610 2014-06-05 02:11
浏览 250
已采纳

Laravel无法显示上传的图像

I am attempting to create a simple file upload system for images, and even after hours of googling, I just can't seem to get it working. Users upload images to my website, I store the path of the image in the database, and then I show the images by making the database path the src of an img tag. Seems simple enough, but it still does not work for me. Anyway, here is my code.

//posting upload in controller
//i named images docs

public function postUpload(){

    Input::file('file')->move(base_path() . '/public/uploads');

    $doc = new Doc();
    $doc->title = Input::get('title');
    $doc->caption = Input::get('caption');
    $doc->path = base_path() . '/public/uploads' . Input::file('file')->getClientOriginalName();

    $doc->save();

}

And here is my index view where I display uploaded images:

@extends('layouts.caselistlayout');

@section('content')


@foreach($docs as $doc)
<h1>{{ $doc->title }}</h1>
<h2>{{ $doc->caption}}</h2>
<img src='{{ $doc->path }}'>

@endforeach

@stop

I can provide the code for my upload view, but I don't think it is needed in this situation.

Thanks for the help!

Edit: Forgot to mention that the exact problem is the images aren't displayed. The title and caption are displayed on the index page, but the image is not displayed. I only see the default HTML placeholder for where the image should be.

Edit 2: File ends up in the folder, and I can see the URL in the path column of the database.

  • 写回答

1条回答 默认 最新

  • dongliao3450 2014-06-05 02:48
    关注

    You are saving full physical path to the database which is causing the issue. You actually need a http address. Example:

    http://example.com/uploads/img1.jpg

    Remove the following line:

      $doc->path = base_path() . '/public/uploads' . Input::file('file')->getClientOriginalName();
    

    Instead, save the following:

      $doc->path = 'uploads/' . Input::file('file')->getClientOriginalName();
    

    and from your view:

    <img src='{{ asset($doc->path) }}'>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?