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

大城市里の小女人 提交于 2020-03-25 17:53:46

问题


This code is returning an strange error:

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

As you can see the file does exist.


回答1:


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




回答2:


Get the file directly from the disk instead of nesting

$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');

Hope this helps




回答3:


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



来源:https://stackoverflow.com/questions/58712947/illuminate-contracts-filesystem-filenotfoundexception-in-laravel-5-6-using-stora

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!