How to get file URL using Storage facade in laravel 5?

前端 未结 13 645
庸人自扰
庸人自扰 2020-11-30 22:40

I\'ve been experimenting using the new Flysystem integration with Laravel 5. I am storing \'localised\' paths to the DB, and getting the Storage facade to complete the path

相关标签:
13条回答
  • 2020-11-30 23:32

    Take a look at this: How to use storage_path() to view an image in laravel 4 . The same applies to Laravel 5:

    Storage is for the file system, and the most part of it is not accessible to the web server. The recommended solution is to store the images somewhere in the public folder (which is the document root), in the public/screenshots/ for example. Then when you want to display them, use asset('screenshots/1.jpg').

    0 讨论(0)
  • 2020-11-30 23:33

    this work for me in 2020 at laravel 7

            $image_resize = Image::make($image->getRealPath());
            $image_resize->resize(800,600);
            $image_resize->save(Storage::disk('episodes')->path('') . $imgname);
    

    so you can use it like this

    echo Storage::disk('public')->path('');
    
    0 讨论(0)
  • 2020-11-30 23:37

    Edit: Solution for L5.2+

    There's a better and more straightforward solution.

    Use Storage::url($filename) to get the full path/URL of a given file. Note that you need to set S3 as your storage filesystem in config/filesystems.php: 'default' => 's3'

    Of course, you can also do Storage::disk('s3')->url($filename) in the same way.

    As you can see in config/filesystems.php there's also a parameter 'cloud' => 's3' defined, that refers to the Cloud filesystem. In case you want to mantain the storage folder in the local server but retrieve/store some files in the cloud use Storage::cloud(), which also has the same filesystem methods, i.e. Storage::cloud()->url($filename).

    The Laravel documentation doesn't mention this method, but if you want to know more about it you can check its source code here.

    0 讨论(0)
  • 2020-11-30 23:40

    Well, weeks ago I made a very similiar question (Get CDN url from uploaded file via Storage): I wanted the CDN url to show the image in my view (as you are requiring ).

    However, after review the package API I confirmed that there is no way do this task. So, my solution was avoid using flysystem. In my case, I needed to play with RackSpace. So, finally decide to create my use package and make my own storage package using The PHP SDK for OpenStack.

    By this way, you have full access for functions that you need like getPublicUrl() in order to get the public URL from a cdn container:

    /** @var DataObject $file */
    $file = \OpenCloud::container('cdn')->getObject('screenshots/1.jpg');
    
    // $url:  https://d224d291-8316ade.ssl.cf1.rackcdn.com/screenshots/1.jpg
    $url = (string) $file->getPublicUrl(UrlType::SSL);
    

    In conclusion, if need to take storage service to another level, then flysystem is not enough. For local purposes, you can try @nXu's solution

    0 讨论(0)
  • 2020-11-30 23:42

    The Path to your Storage disk would be :

    $storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()
    

    I don't know any shorter solutions to that...

    You could share the $storagePath to your Views and then just call

    $storagePath."/myImg.jpg";
    
    0 讨论(0)
  • 2020-11-30 23:42

    If you need absolute URL of the file, use below code:

    $file_path = \Storage::url($filename);
    $url = asset($file_path);
    // Output: http://example.com/storage/filename.jpg
    
    0 讨论(0)
提交回复
热议问题