Laravel文件上传路径和视图渲染

时间:2014-07-12 13:26:20

标签: laravel laravel-4

我成功上传了一个文件并使用以下代码段存储其路径:

/*Image Handling*/
                $file = Input::file('profilePicture');
                $destinationPath = public_path().'/images/';
                $filename = $file->getClientOriginalName();
                Input::file('profilePicture')->move($destinationPath, $filename);
                //Profile image
                $profileImg = $destinationPath.$filename;

然后我将profileImg存储在数据库中。这就是它的样子:

/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg

现在我想在其中一个视图中显示这张照片。所以我这样做了:

<a class="th" href="{{URL::to('/')}}">{{ HTML::image($details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}</a>        

以及它的呈现方式:

<a class="th" href="http://localhost:8888/devproject/index.php"><img src="http://localhost:8888/devproject/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg" style="width:100px;height:100px;" alt="work"></a>    

这当然不起作用,因为路径不正确,但它是如何存储的。我需要图像的路径:

/public/images/picture.jpeg

而不是:

/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg

因为这将适合网址并显示图片。任何关于如何实现这一点的建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

不要将整个路径保存到模型中,只保存文件名:

$profileImg = $filename;

然后,不要单独使用$details->profileImg,而是使用:

asset('images/' . $details->profileImg)

即:

{{ HTML::image('images/' . $details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}
相关问题