在路径中找不到 Laravel 文件,但它确实存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44769693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:13:06  来源:igfitidea点击:

Laravel File not found at path but it does exist

phplaravelfilestorage

提问by kjdion84

Getting the following error in console when attempting to get a files contents:

尝试获取文件内容时,在控制台中出现以下错误:

[League\Flysystem\FileNotFoundException] File not found at path: C:/wamp64/www/lion/resources/generate/json/Car.json

[League\Flysystem\FileNotFoundException] 在路径找不到文件:C:/wamp64/www/lion/resources/generate/json/Car.json

When I copy and paste that exact path in explorer it opens the json file fine.

当我在资源管理器中复制并粘贴该确切路径时,它可以很好地打开 json 文件。

Here is my code:

这是我的代码:

$this->json = json_encode(Storage::get(resource_path('generate/json/'.$this->argument('model').'.json')));

采纳答案by kjdion84

I figured it out.

我想到了。

Storage::getactually uses the path relative to the filesystem disk configuration, therefore the error message itself is misleading.

Storage::get实际上使用相对于文件系统磁盘配置的路径,因此错误消息本身具有误导性。

I've corrected the issue by simply using file_get_contents()instead.

我已经通过简单地使用file_get_contents()来纠正了这个问题。

回答by Gluten

As others pointed out Storage::getindeed uses the path relative to the filesystem disk configuration, by default the localdriver is used (config/filesystems.php):

正如其他人指出的那样,Storage::get确实使用了相对于文件系统磁盘配置的路径,默认情况下使用local驱动程序 ( config/filesystems.php):

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

So if you are using the localdriver and your file resides at app/public/yourfile.extfor example then the call to Storageshould be:

因此,如果您正在使用local驱动程序并且您的文件驻留在app/public/yourfile.ext例如,那么调用Storage应该是:

Storage::get('public/yourfile.ext');

回答by Manish Champaneri

Try this

尝试这个

$this->json = json_encode(app_path('/resources/generate/json/Car.json');