-1

I have some problems with how to read from external json file in php.

I tried this solution:

$data = json_decode(file_get_contents('/data/usersdata.json'),TRUE);
print_r ($data);

In the usersdata.json now is:

{"name":"admin","password":"admin","permission":"admin"}

My problem is, I can't see anything on the screen, I want to put data from my json file to an $data array, or something to be usable.

Thamilhan
  • 12,351
  • 5
  • 33
  • 59
gabor aron
  • 166
  • 1
  • 2
  • 10
  • use `print_r($data)` and check, it is decode properly or not. – er.irfankhan11 Apr 30 '16 at 14:32
  • 2
    Assuming `/data/usersdata.json` exists, your code _should_ work. – John Weisz Apr 30 '16 at 14:33
  • 1
    If it does exists, check the permissions. If you're running it from a web server, the user trying to access it might be `apache` or `nginx` or something. – toster-cx Apr 30 '16 at 14:36
  • 3
    Try `data/usersdata.json` I think data is not your root directory. – Thamilhan Apr 30 '16 at 14:36
  • Possible duplicate of [I can't read from .json file to append it](http://stackoverflow.com/questions/36953579/i-cant-read-from-json-file-to-append-it) – fusion3k Apr 30 '16 at 14:36
  • I tried on xampp 3.2.2 – gabor aron Apr 30 '16 at 14:37
  • You've already asked [this question](http://stackoverflow.com/questions/36953579/i-cant-read-from-json-file-to-append-it). You have to use absolute or relative path. “/data/usersdata.json” is invalid path for your OS. If you activate error reporting, you will see the error. – fusion3k Apr 30 '16 at 14:41
  • Sorry, but no, that's an another thing, because here I want to read from json, and there I got an answer which use "file_put_contents" but that not make an valid json... and that's a different problem. At this problem, i don't see anything on the screen, and in the code of page the body is empty... – gabor aron Apr 30 '16 at 14:48
  • It is the exact question, and the answer is the same. Activate error reporting (`error_reporting( E_ALL ); ini_set( 'display_errors', 1 );` at top of your script). You will se *PHP Warning: file_get_contents(/data/usersdata.json): failed to open stream: No such file or directory* – fusion3k Apr 30 '16 at 14:57
  • Ok, but how can I concatenate these things, or how can I put in file to be an valid json after append? – gabor aron Apr 30 '16 at 15:07

1 Answers1

1

Tested on PHP 5.6 (MAMP)

I replicated your example and everything works as expected

try {
    $fileData = file_get_contents('src/data.json');
    $data = json_decode($fileData, true);

    print_r($data);
    
} catch(Exception $e) {
    echo $e->getMessage();
}

Output:

Array ( [name] => admin [password] => admin [permission] => admin )
  • Start using try/catch statements to catch any non-fatal errors
  • Make sure error_reporting is set to E_ALL
  • In development mode/stage avoid nesting functions as it makes debugging difficult and less readable. With few lines of code saving a line here and there will not impact your performance.
Community
  • 1
  • 1
dchayka
  • 1,201
  • 11
  • 19