【问题标题】:Illuminate\Contracts\Filesystem\FileNotFoundException in Laravel 5.6 using Storage facadeIlluminate\Contracts\Filesystem\FileNotFoundException 在 Laravel 5.6 中使用存储门面
【发布时间】:2019-11-05 13:59:55
【问题描述】:

这段代码返回一个奇怪的错误:

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

如您所见,该文件确实存在。

【问题讨论】:

    标签: laravel linux-mint


    【解决方案1】:

    你需要得到如下代码的文件:

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

    以上has方法可用于确定磁盘上是否存在给定文件:

    请阅读文档https://laravel.com/docs/5.1/filesystem#retrieving-files

    【讨论】:

      【解决方案2】:

      直接从磁盘获取文件而不是嵌套

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

      如果你没有在filesystems.php 中设置notas_xml 磁盘

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

      要使用你的代码,你需要像config/filesystems.php这样设置一个磁盘

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

      然后像这样简单地获取文件

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

      希望对你有帮助

      【讨论】:

      • 我执行了所有步骤。这个错误没有意义。
      • 函数中的 URL 似乎不正确,你能分享你所有的代码吗?包括路线
      【解决方案3】:

      为了更好地理解这一切......诀窍在于:config/filesystems.php

      如果你有这个代码(这是 Github 中 Laravel 的默认值)

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

      此门面Storage 将在文件夹中起作用

      root_laravel_project/storage/app

      所以如果你想检查“israel.txt”文件是否存在
      if( Storage::exists('israel.txt') ){ echo "File found. And it exists on the path: root_laravel_project/storage/app/israel.txt"; }

      请记住,到目前为止,它与符号链接 php artisan storage: link 无关

      这个符号链接只是为了让“storage”文件夹中一个名为“public”的文件夹成为通过HTTP进行公共访问的一部分

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

      然后在做 sym 的时候。您可以通过 http 访问文件(对任何用户都是公开的)
      此示例假设您使用的是虚拟主机(如果不是,您必须这样做作为在本地更好地工作的建议)我>

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

      或者这样更好地理解为没有虚拟主机的老派

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

      然后这些网址将在您的文件夹中查找

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

      Laravel 的文档有点简短,可能会让人对这个问题感到困惑。但你只需要记住,一件事是通过“存储门面”访问(这是上传和验证是否有文件的正确方法),另一件事是通过 http(通过 url)访问,这是符号链接(例如,您已经给予用户下载文件或查看它是否为 PDF 的处理方式)。

      我希望它有所帮助。美好的一天

      【讨论】:

        猜你喜欢
        • 2018-12-15
        • 2022-10-17
        • 2020-06-08
        • 2021-09-15
        • 1970-01-01
        • 2021-06-24
        • 1970-01-01
        • 2022-07-05
        • 2019-01-29
        相关资源
        最近更新 更多