【发布时间】:2016-03-19 08:53:08
【问题描述】:
我使用 Symfony 网站文档提供的代码示例在 Symfony2 中创建文件上传功能
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
// return __DIR__.'/../../../../web/'.$this->getUploadDir();
return __DIR__.'/../../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
我可以上传文件但twig无法显示它
<td><img src="/uploads/documents/{{ task.path }}" id="img-responsive"> </td>
使用 Google 的开发者工具检查显示图像的正确链接
<img src="/uploads/documents/Desert.jpg" id="img-responsive">
复制图片url到浏览器显示
Object not found
404
在这种情况下,您将如何显示在 Symfony 中上传的图像?
app
src
web
bundles
uploads
documents
return __DIR__.'/../../../../../web/'.$this->getUploadDir();
更新
路径保存在数据库中
path id
Desert.jpg 1
cat.jpg 2
dog.jpg 3
【问题讨论】:
-
你真的应该使用 twigs
{{ asset() }}方法。然后在其中使用路径和文件名。
标签: symfony