I can't read my json file inside codeigniter controller, it returns an error.
My code:
$curYear = date("Y");
$string = file_get_contents(base_url("assets/data/".$curYear."_cal.json"));
Error Message return by my controller trying to read json file using:
file_get_contents(//localhost/EBR_Labeling/assets/data/2018_cal.json): failed to open stream: No such file or directory
Using this syntax:
file_get_contents("./assets/data/".$curYear."_cal.json")
Please use this code --
<?php
$json = file_get_contents(FILE_PATH);
$obj = json_decode($json);
echo '<pre>' . print_r($obj) . '</pre>';
?>
And let me know If you need any other help.
Thanks
We can simply store all json in views folder and can access it directly as follow:-
<?php
$countries = $this->load->view('countries.json', '', true) //this will load countries.json
$countries = json_decode($countries);
?>
Points to be noted : -
$this->load->view('countries') is same as writing $this->load->view('countries.php')
And this loads the view in browser and doesn't store it in variable. When we want some another type of file to be loaded, then we can simply change the extension.$this->load->view('countries', $data, true);
Passing third parameter as true allows us to store the view in variable. If we don't want to pass second parameter then we can pass empty string as $this->load->view('countries', '', true);If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With