Laravel将图像链接到基本路径

时间:2015-01-01 22:47:34

标签: php laravel public-html

希望将所有用户上传的图片移到公开视图之外。

我的结构如此:

Mamp
  htdocs
    buildsanctuary
      /app
      /public_html
        / images
      /user_images

我以前用图片链接到图片

{{ asset('/')}} images/linktoimage.jpeg

但是我现在已经像这样联系了:

<?php echo base_path() ?> /user_images/linktoimage.jpeg

然而,这不起作用,并且显示无法找到图像。

在浏览器中检查图像链接时显示:

http://localhost/Applications/MAMP/htdocs/buildsanctuary/user_images/linktoimage.jpeg

所以我不确定为什么这不起作用?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

因为浏览器无法直接访问公共目录之外的文件 如果可以,我会将它们留在公共文件夹中。但是,如果您确实需要移动它们(例如,为了保护它们只有经过身份验证的用户才能查看图片)您可以创建一个路径,然后读取该文件并将其返回:

Route::get('user_images/{filename}', array('as' => 'user_images', function($filename){
    if(!File::exists($path)){
        App::abort(404);
    }
    $guesser = Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser::getInstance();
    $mimeType = $guesser->guess($path);
    return Response::make(File::get($path), 200, array('Content-Type' => $mimeType));
}))->where('filename', '.*');

现在您可以在视图中使用它:

{{ route('user_images', 'linktoimage.jpeg') }}
相关问题