【问题标题】:[ Laravel 5.2 ]Storage file not found even if it exists[ Laravel 5.2 ]即使存在也找不到存储文件
【发布时间】:2016-06-29 13:22:34
【问题描述】:
\Storage::disk('verify_files')->put('verify-' . $fileCode[3], $fileCode[0]);

    $attach = \Storage::get(storage_path('app/public/verify') . '/' . 'verify-' . $fileCode[3]);

我正在使用上面的代码来生成和获取文件。

这是我使用的文件系统:

'verify_files' => [
        'driver' => 'local',
        'root' => storage_path('app/public/verify'),
        'visibility' => 'public',
    ],

当我用 file_exists() 测试它时,它返回 true 但 laravel 返回 FileNotFoundException in FilesystemAdapter.php line 61:

我错过了什么吗?

【问题讨论】:

    标签: php laravel storage file-not-found


    【解决方案1】:

    Storage 门面的get 方法会自动查找默认磁盘的目录。在这种情况下,您使用storage_path 传递文件的绝对路径,这是多余的,因为它会按原样查看storage/app 目录。

    改为Storage::disk('verify_files')->get('verify-' . $fileCode[3]);

    当您需要文件和目录的绝对路径时使用storage_path

    【讨论】:

    • 我传递了路径而不是使用 Storage 门面并从那里处理它。当您的答案有效时,我会更新您。谢谢!
    • 快一个月了,忘记更新了。问题是 ->get() 获取内容。我用 storage_path('app/public/verify') 之类的东西来获取文件。 “/”。 $file 如果有更好的解决方案,请告诉!
    【解决方案2】:

    我遇到了和你一样的错误,只是我使用了与你不同的方法:

                $parse = new Parsedown();
                $url = url('about.md'); // This would fail, because it returns absolute
                $url = 'about.md'; // the file is living in the root directory of public. This would succeed, because it's relative
    
                try { // catch file not found errors
                    $about = File::get($url);
                } catch (Illuminate\Filesystem\FileNotFoundException $exception) {
                    die ('Bad File');
                }
    
                echo $parse->text($about);
    

    这来自this page 和上面@SArnab 提到路径不需要是绝对的。

    除了我使用不同的方法之外,我之前遇到的唯一问题是我使用的是绝对路径,而不是我现在使用的相对路径。

    我只是想在 Laravel 5.2 中添加这种替代方法来获取文件内容。

    享受吧!

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 2014-02-20
      • 2018-12-04
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      相关资源
      最近更新 更多