Diego Alves
Diego Alves

Reputation: 2677

Illuminate\Contracts\Filesystem\FileNotFoundException in Laravel 5.6 using Storage facade

This code is returning an strange error:

$file = Storage::get(Storage::disk('notas_xml')->path('') . 't.txt');

enter image description here

As you can see the file does exist.

Upvotes: 0

Views: 7621

Answers (3)

Salim Djerbouh
Salim Djerbouh

Reputation: 11044

Get the file directly from the disk

$exists = Storage::disk('notas_xml')->exists('t.txt');
if ($exists) {
  $file = Storage::disk('notas_xml')->get('t.txt');
}

And if you didn't setup notas_xml disk in filesystems.php

$file = Storage::get('public/arquivos/notas_xml/t.txt');

And to use your code, you need to setup a disk like so in config/filesystems.php

'notas_xml' => [
    'driver' => 'local',
    'root' => storage_path('app/public/arquivos/notas_xml'),
    'url' => env('APP_URL') . '/storage',
    'visibility' => 'public',
],

And get the file simply like this

$file = Storage::disk('notas_xml')->get('t.txt');

Upvotes: 2

Israel Alvarez
Israel Alvarez

Reputation: 11

To better understand all this... The trick is in: config/filesystems.php

If you have this code (which is the default value of Laravel in Github)

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

This Facade Storage will act in the folders

root_laravel_project/storage/app

So if you want to check if an "israel.txt" file exists
if( Storage::exists('israel.txt') ){ echo "File found. And it exists on the path: root_laravel_project/storage/app/israel.txt"; }

Remember that up to this point it has nothing to do with the symbolic link php artisan storage: link

This symbolic link is only to make a folder called "public" within the "storage" folder be part of the public access through HTTP

    'disks' => [
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

Then at the time of doing the sym. You can access the files by http (which are public for any user)
This example assumes that you are using a virtual host (and if not, you must do it as a recommendation for work better locally)

http:// root_laravel_project.test/storage/israel-alvarez.txt

Or so that it is better understood as in the old school without a virtual host

http:// localhost/public/storage/israel-alvarez.txt

Then these urls will look inside your folder

root_laravel_project/storage/app/public/israel-alvarez.txt

Laravel's documentation is somewhat brief and can be confusing regarding this issue. But you just have to remember that one thing is to access through the "storage Facade" (which is the correct way to upload and verify if there are files) and another thing is to access through the http (through url) which is the symbolic link (which is the treatment you already give to users to download files or see if it is a PDF for example).

I hope it helps. Good day

Upvotes: 1

Amit Senjaliya
Amit Senjaliya

Reputation: 2945

You need to get the file as below code:

Storage::disk('notas_xml')->has('t.txt');

Above has method may be used to determine if a given file exists on the disk:

Please read documentation https://laravel.com/docs/5.1/filesystem#retrieving-files

Upvotes: 1

Related Questions