How to do a Json_Decode of a file in json format

1

Hello,

I'm currently storing json data in a file using fopen.

$json = fopen("$root/apil/Controllers/json/" . $this->getX_id() . ".json", "w+"); $customers_results = $customers->fetchAll(PDO::FETCH_OBJ); foreach ($customers_results as $customer) { array_push($file_content , $customer->x_id); } fwrite($json, json_encode($file_content)); fclose($json);

So far, the file is stored in JSON format correctly:

AndasexpectedIusethissamefileinanotherclass,however,byperformingajson_decodeofthe$filevariablethat"comes" from fopen me the following error is returned:

Warning: json_decode() expects parameter 1 to be string, resource given in ...

The code I use to collect the data from the file and later to do a json_decode is this:

$root = $_SERVER['DOCUMENT_ROOT']; $x_id = $this->x_id(); $y_id = $this->y_id(); $file = fopen("$root/apil/Controllers/json/" . $y_id . ".json", "w+"); $aux = json_decode($file); fclose($file); ...

It is worth mentioning that after searching for solutions in Forums I also tried to use json_decode as follows $aux = json_decode(json_encode($file)); in this case I am not returned any errors, however, using json_last_error() me is returned error 4 that according to the documentation it means JSON_ERROR_SYNTAX , it is worth mentioning that the variable $y_id also has the correct content, which in this case is the name of my file.

Thank you in advance if anyone can help me with this problem.

    
asked by anonymous 25.09.2018 / 16:47

0 answers