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

太空猴

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

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

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

Leng

不要保存模型的完整路径,只保存文件名:

$profileImg = $filename;

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

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

IE:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章