doumengwei0138 2015-01-01 22:47
浏览 115

Laravel将图像链接到基本路径

looking to move all of my user uploaded images outside of the public view.

I have my structure as so:

Mamp
  htdocs
    buildsanctuary
      /app
      /public_html
        / images
      /user_images

I used to link to images with:

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

However I have now linked like so:

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

This does not work however and it shows as the image could not be found.

When checking the link to the image in a browser it shows:

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

So im not sure why this isnt working? Any ideas?

  • 写回答

1条回答 默认 最新

  • dsgdhf5674 2015-01-01 23:47
    关注

    Because files outside the public directory are not directly accessible for the browser.
    If you can I would leave them in the public folder. However if you really need to move them (e.g. to protect them that only authenticated users can view the pictures) You can create a route that then reads the file and returns it:

    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', '.*');
    

    Now you can use this in your views:

    {{ route('user_images', 'linktoimage.jpeg') }}
    
    评论

报告相同问题?