Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Storage::get()会导致FileNotFoundException_Php_Json_Laravel - Fatal编程技术网

Php Storage::get()会导致FileNotFoundException

Php Storage::get()会导致FileNotFoundException,php,json,laravel,Php,Json,Laravel,我正在尝试将一个外部JSON文件加载到我的laravel应用程序中。下面是我的代码片段 $location = __DIR__.'/config.json'; echo $location; // Results in correct file location echo File::exists($location); // Return 1 echo Storage::get($location); // Throws a FileNotFound Excepti

我正在尝试将一个外部JSON文件加载到我的laravel应用程序中。下面是我的代码片段

$location = __DIR__.'/config.json';
echo $location;               // Results in correct file location
echo File::exists($location); // Return 1
echo Storage::get($location); // Throws a FileNotFound Exception
File::exists()
方法如何返回
true
Storage::get()
如何抛出异常

我的
config/filesystems.php
文件:

<?php

return [

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    'cloud' => env('FILESYSTEM_CLOUD', 's3'),

    'disks' => [

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

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

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

    ],

];

为什么要为此使用
存储
类?该类专门用于访问存储文件夹或外部文件服务器。您将文件存储在控制器/代码的旁边,因此使用老式方法可能更容易

$location = __DIR__.'/config.json';

if (File::exists($location)) {
    $string = file_get_contents($location);
    $json = json_decode($string, true);

    // logic
} else {
    // Do something
}

您可以通过以下方式尝试通过文件facade访问它:

File::get($location);
该错误可能是因为存储试图从文件的默认路径(./project/Storage/app)以及您发送的路径访问该文件。我最好的建议是添加一个新磁盘 像这样

// config/filesystems.php

'customDrive' => [
   'driver' => 'local',
   'root' => base_path(), // here put your full path
],
并顺利通过

Storage::disk("customDrive")->get("config.json");

Source

能否显示您的
config/filesystems.php
文件?(使用
磁盘
键)。将其添加到主POST没有问题。再看看塞尔吉奥·赖斯的答案,他的答案更为夸张,而我的答案则不然;)