Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message: file_get_contents(sso.json): failed to open stream: No such file or directory

Tags:

json

php

I'm trying to get data from JSON file manually, not from URL. I use function file_get_contents. And I got error :

Message: file_get_contents(sso.json): failed to open stream: No such file or directory

Even though the path is correct. This is my structure project :

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

This is my code in function user() in file Kalender.php

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

    echo $characters['name'];
}

And I call the controller in browser localhost/kalenderkerja/agendakerja/kalender/user and I got this error : enter image description here

like image 322
syaifulhusein Avatar asked Mar 06 '23 00:03

syaifulhusein


1 Answers

If you change to

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

This should work. You can learn more about magic constants from HERE

like image 145
HOSSAIN AZAD Avatar answered Mar 07 '23 22:03

HOSSAIN AZAD