Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. For example I store screenshots/1.jpg and using

Storage::disk('local')->get('screenshots/1.jpg') 

or

Storage::disk('s3')->get('screenshots/1.jpg')  

I can retrieve the same file on different disks.

get retrieves the file contents, but I am hoping to use it in my views like this:

<img src="{{ Storage::path('screenshots/1.jpg') }}" /> 

but path, or anything able to retrieve the full path is not available (as far as I can see). So how can I return the full path? Or, I'm wondering if this is by design? If so, why am I not supposed to be able to get the full path? Or, am I going about this completely the wrong way?

like image 283
daviestar Avatar asked Mar 10 '15 13:03

daviestar


People also ask

How do I get the path of a file in Laravel storage?

Retrieve the file pathphp $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();

What is __ DIR __ In Laravel?

__DIR__ is the current directory you are in, if you wanted to go back one step, you could use dirname. This is used such as; dirname(__DIR__);

What is Storage_path?

The storage_path function returns the fully qualified path to the storage directory: $path = storage_path(); You may also use the storage_path function to generate a fully qualified path to a given file relative to the storage directory: $app_path = storage_path('app'); $file_path = storage_path('app/file.txt');

How do I get files in Laravel?

If you have file object from request then you can simply get by laravel function. $extension = $request->file->extension(); dd($extension); If you have file object from request then you can simply get by laravel function.


1 Answers

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"; 
like image 143
shock_gone_wild Avatar answered Sep 28 '22 05:09

shock_gone_wild