rosscooper
rosscooper

Reputation: 2045

Laravel 5.3 Filesystem store/putFile not working

I am having a problem using the filesystem in Laravel 5.3. I am trying to save a file to my local storage but when I call either $file->store('directory', 'local') or Storage::putFile('directory', $file) it stores in the correct location storage/app/directory/filename but the path returned by both functions does not include the path to the storage/app directory, just directory/filename.

My local storage driver config is as below:

'local' => [
            'driver' => 'local',
            'root'   => storage_path('app'),
        ],

Obviously I could wrap the $path with the storage_path() helper but this doesn't seem right when reading the docs. https://laravel.com/docs/5.3/filesystem#storing-files

Am I doing wrong or missing something?

Upvotes: 0

Views: 1527

Answers (2)

Paras
Paras

Reputation: 9455

This is by design because in your config, you've already set the storage/app directory as your root directory.

So, it returns the path relative to your root.

Take the case that you want it to return absolute rather than relative paths. Now imagine if your Storage driver was an AWS S3 bucket. In this case there is no absolute path. The absolute path is a term specific to the local driver but the code was written to be extended/used by multiple drivers (and hence they wanted a consistent return value for the putfile method). So, relative path was the only choice in this case

Upvotes: 0

it's not actually crazy. The storage path is configurable, therefore if Laravel were to store it in database but you changed it down the line, there would be an issue.

What you can do simply is add storage_path($file) when you try to display your file in your blade template. That will automatically apply the proper path to storage before the file name and should find your file where it is.

Tell me if that helped/worked

Upvotes: 0

Related Questions