Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_put_contents doesn't work with json laravel & php

I tried everything to push item into json file but when call file_put_contents("file.json",$data) doesn't work and the page shutdown.

Here's 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

like image 696
StAx Avatar asked Jul 14 '15 04:07

StAx


1 Answers

It could be a permission problem

Is the directory /js chmoded to 777? sometimes php won't allow you to access directories if they don't have enough permission. I don't know about blank errors though.

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

like image 193
Jigs Virani Avatar answered Sep 21 '22 10:09

Jigs Virani