首页 > 解决方案 > 消息:file_get_contents(sso.json):无法打开流:没有这样的文件或目录

问题描述

我正在尝试手动从 JSON 文件中获取数据,而不是从 URL 中获取数据。我使用函数file_get_contents。我得到了错误:

消息:file_get_contents(sso.json):无法打开流:没有这样的文件或目录

即使路径是正确的。这是我的结构项目:

-> kalenderkerja
   -> application
      -> controllers
         -> agendakerja
            Kalender.php
            sso.json
   -> assets
   -> ...

这是我在文件Kalender.php中的函数user()中的代码

public function user() {
    $url = 'sso.json';
    $data = file_get_contents($url);
    $characters = json_decode($data, true);

    echo $characters['name'];
}

我在浏览器localhost/kalenderkerja/agendakerja/kalender/user中调用控制器 ,我得到了这个错误: 在此处输入图像描述

标签: phpjson

解决方案


如果您更改为

$url = __DIR__.'/sso.json';

这应该有效。您可以从这里了解更多关于魔法常数的信息


推荐阅读