How to Get File Url Using Storage Facade in Laravel 5

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

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.

Get path to file in storage

There is no correct way to do this; because it should not be done. The Storage is an opaque system to talk to different storage systems; as such there is no api to get the backing file path. As an example, that wouldn't work with Amazon S3. The only path your application knows about is the string you send to the Storage facade to work with the file, there are no guarantees that this string is used to generate the filename when the storage system stores the file.

There are some hacks you can use that works for the local disk, but those are not available for the Storage system in general. Using these solutions means that you'll limit yourself to only use the local disk; this will cause you troubles when you need to scale out and add another server. You'll then have two servers with two separate local disks, with separate content.

The correct way to work with the files, that will work for all configurations, is to get the file content (Storage::get), do the modifications (including storing them in a temporary file) and then write back the new file content (Storage::set).

If you're really sure that you will only ever use the local filesystem, use the File facade instead of the Storage facade. I'm unable to find any documentation for this, only the interface it exposes.

Reference: https://github.com/laravel/framework/issues/13610

Laravel Public url for storage files

What about Storage::url? It works even for local storage.

You can find more here: https://laravel.com/docs/5.4/filesystem#file-urls

If you want to return all files' urls from the directory, you can do something like this:

return collect(Storage::files($directory))->map(function($file) {
return Storage::url($file);
})

Don't forget to inject the \Illuminate\Filesystem\FilesystemManager instead of the Storage facade if you are looking for a not-facade way.

Edit:

There are 2 (or more) ways how you can deal with modified date:

Pass files to the view.

|ou can pass your Storage::files($directory) directly to the view and then in your template use following:

// controller:

return view('view', ['files' => Storage::files($directory)]);

// template:

@foreach($files as $file)
{{ Storage::url($file) }} - {{ $file->lastModified }} // I'm not sure about lastModified property, but you get the point
@endforeach

Return an array:

return collect(Storage::files($directory))->map(function($file) {
return [
'file' => Storage::url($file),
'modified' => $file->lastModified // or something like this
]
})->toArray()

How to open file from storage by URL address?

Files in the storage are not accessible by url by default

You could use Public Disk. For that you need to create symbolic link from public/storage to storage/app/public

From Larvel 5.3 and up, you have artisan command that will help you to create symlink

php artisan storage:link

If you are using older version of Laravel, you can find an answer how to create symbolic link here


Another approach would be to create new disk "uploads", by editing the file config/filesystems.php

'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]

To store files in this location

Storage::disk('uploads')->put($file_name, $file_content);

And to get the file

asset('uploads/'. $file_name)

Laravel - link to a file saved in storage folder

UPDATE:

According to laravel docs, You can get the URL to the file like this:

use Illuminate\Support\Facades\Storage;

$url = Storage::url('file1.jpg');

Remember, if you are using the local driver, all files that should be
publicly accessible should be placed in the storage/app/public
directory. Furthermore, you should create a symbolic link at
public/storage which points to the storage/app/public directory.

Hope this helps!



Related Topics



Leave a reply



Submit