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


Problems displaying uploaded images in Symfony 2

我按照这些文档在我的Symfony项目中做了一个简单的小图像上传页面。 将目标目录的权限设置为 777 后,文件上传工作正常。 不幸的是,当我尝试在图库中显示上传的图像时,它们不会显示,并且 Apache 2 给了我一个"未找到"错误。

图像的 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';
}

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

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

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

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

即使再次,目标目录的权限为 777,我可以文件系统中看到图像。

知道为什么它告诉我找不到我的图像吗?

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

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