file_put_contents doesn't work with json laravel & php
Asked Answered
M

1

7

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

Marinara answered 14/7, 2015 at 4:55 Comment(3)
have you check 777 permissions and Apache permissionsMudd
$temp = file_put_contents('js/data.json', $arr); and echo $temp. and see it returns true or false;Mudd
yes, last night think about it, permission problem. ty C:Marinara
M
1

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.

Mudd answered 14/7, 2015 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.