在Symfony 2中显示上传图像的问题

时间:2014-11-13 22:57:31

标签: php symfony

我在these docs之后的Symfony项目中创建了一个简单的小图片上传页面。将目标目录的权限设置为777后,文件上传工作正常。不幸的是,当我尝试在库中显示上传的图像时,他们不会显示,而Apache 2给了我一个未找到的& #39;错误。

图像的Web路径由我的GalleryImage实体动态生成:

public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded
    // documents should be saved
    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/gallery';
}

在我的视图(twig)中,我尝试使用以下内容显示它们:

<a href="{{ image.webPath }}" data-lightbox="gallery"><img src="{{ asset(image.webPath) }}" class="gallery-thumb"></a>

生成的标记如下:

<a href="/home/kevin/www/diva/src/MajorProductions/SewingDiva/SiteBundle/Entity/../../../../../web/uploads/gallery/d15728da272656a4ab0e670f589ee033e43494d6.jpeg" data-lightbox="gallery"><img src="/home/kevin/www/diva/src/MajorProductions/SewingDiva/SiteBundle/Entity/../../../../../web/uploads/gallery/d15728da272656a4ab0e670f589ee033e43494d6.jpeg" class="gallery-thumb"></a>

但是,当我尝试访问它们时,它会告诉我:

  

在此服务器上找不到请求的网址/home/kevin/www/diva/web/uploads/gallery/d15728da272656a4ab0e670f589ee033e43494d6.jpeg

即使目标目录的权限也是777,我也可以在文件系统中看到那些图像。

为什么它告诉我我的图像无法找到?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这对我有用:

<a href="{{ asset('uploads/gallery/') }}{{image.path}}" data-lightbox="gallery"><img src="{{ asset('uploads/gallery/') }}{{image.path}}" class="gallery-thumb"></a>