Laravel 5 - How to access image uploaded in storage within View?

前端 未结 12 2553
悲哀的现实
悲哀的现实 2020-11-22 14:05

I have got user\'s avatars uploaded in Laravel storage. How can I access them and render them in a view?

The server is pointing all requests to /public,

相关标签:
12条回答
  • 2020-11-22 14:12

    If you are like me and you somehow have full file paths (I did some glob() pattern matching on required photos so I do pretty much end up with full file paths), and your storage setup is well linked (i.e. such that your paths have the string storage/app/public/), then you can use my little dirty hack below :p)

     public static function hackoutFileFromStorageFolder($fullfilePath) {
            if (strpos($fullfilePath, 'storage/app/public/')) {
               $fileParts = explode('storage/app/public/', $fullfilePath);
               if( count($fileParts) > 1){
                   return $fileParts[1];
               }
            }
    
            return '';
        }
    
    0 讨论(0)
  • 2020-11-22 14:15

    According to Laravel 5.2 docs, your publicly accessible files should be put in directory

    storage/app/public
    

    To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

    ln -s /path/to/laravel/storage/app/public /path/to/laravel/public/storage
    

    Now you can create in your view an URL to the files using the asset helper:

    echo asset('storage/file.txt');
    
    0 讨论(0)
  • 2020-11-22 14:21

    If you are on windows you can run this command on cmd:

    mklink /j /path/to/laravel/public/avatars /path/to/laravel/storage/avatars 
    

    from: http://www.sevenforums.com/tutorials/278262-mklink-create-use-links-windows.html

    0 讨论(0)
  • 2020-11-22 14:22

    without site name

    {{Storage::url($photoLink)}}
    

    if you want to add site name to it example to append on api JSON felids

     public function getPhotoFullLinkAttribute()
    {
        return env('APP_URL', false).Storage::url($this->attributes['avatar']) ;
    }
    
    0 讨论(0)
  • 2020-11-22 14:23

    If disk 'local' is not working for you then try this :

    1. Change local to public in 'default' => env('FILESYSTEM_DRIVER', 'public'), from project_folder/config/filesystem.php
    2. Clear config cache php artisan config:clear
    3. Then create sym link php artisan storage:link

    To get url of uploaded image you can use this Storage::url('iamge_name.jpg');

    0 讨论(0)
  • 2020-11-22 14:23

    You can run this command in your console to make link:

    php artisan storage:link
    

    Reference link

    0 讨论(0)
提交回复
热议问题