File_put_contents not working with json laravel & php

I tried everything to insert an element into the json file, but when calling file_put_contents("file.json",$data) page file_put_contents("file.json",$data) off and does not work.

Here is my controller code.

 public function addJson() { $titulo = Input::get('title'); $name = Input::file('image')->getClientOriginalName(); $arr = json_decode(file_get_contents("js/data.json"), true); array_push($arr['galerias'], array('id' => count($arr['galerias'])+1, 'name' => $name, 'img' => 'galeria/'.$name)); $arr2 = $arr['galerias']; $arr = json_encode($arr); echo $arr; file_put_contents('js/data.json', $arr); return view('main.prueba')->with('name',$arr2); } 

And here is the json file.

 { "galerias": [ { "id": 1, "name": "Fiesta Privada", "img": "galeria/FP.JPG" }, { "id": 2, "name": "Fiesta Privada", "img": "galeria/FP2.JPG" }, { "id": 3, "name": "Fiesta Privada", "img": "galeria/FP3.JPG" }, { "id": 4, "name": "Fiesta Privada", "img": "galeria/FP4.JPG" }, { "id": 5, "name": "Fiesta Privada", "img": "galeria/FP5.JPG" }, { "id": 6, "name": "Fiesta Privada", "img": "galeria/FP6.JPG" }, { "id": 7, "name": "Los Kjarkas", "img": "galeria/Los_Kjarkas.jpg" }, { "id": 8, "name": "Palmenia Pizarro", "img": "galeria/Palmenia_Pizarro.jpg" }, { "id": 9, "name": "Palmenia Pizarro", "img": "galeria/Palmenia_Pizarro_2.jpg" }, { "id": 10, "name": "Palmenia Pizarro", "img": "galeria/Palmenia_Pizarro_3.jpg" } ] } 

Heeeelp me pls: C

+7
json php laravel file-put-contents
source share
1 answer

This may be a resolution issue.

Is /js directory changed to 777? sometimes php will not allow you to access directories if they have insufficient rights. However, I do not know about empty errors.

Try to check if it has enough permissions, if not, then set it to 777 and try.

+1
source share

All Articles