0

I try to add data to a json file.

First try:

$filename = "$root/nachrichten/bla.json";

$neueartikel = json_decode(file_get_contents($filename), true); /*until here everything works, I can loop the file*/

$neueartikel[] = array('titel' => 'bla',
                    'bild' => 'bla',
                    'url' => 'bla',
                    'ID' => 'bla',
                    'bildserver' => 'bla'); 

file_put_contents('$root/nachrichten/bla.json', json_encode($neueartikel);

Second try:

$filename = "$root/nachrichten/bla.json";

$neueartikel = json_decode(file_get_contents($filename), true);

$neu[] = array('titel' => 'bla',
                    'bild' => 'bla',
                    'url' => 'bla',
                    'ID' => 'bla',
                    'bildserver' => 'bla');

$result = array_merge($neueartikel, $neu);

file_put_contents('$root/nachrichten/bla.json', json_encode($result);

How can I do this right????

UPDATE:

I also tryed what PotatoIng sayd:

$filename = "$root/nachrichten/bla.json";

    $temparray = json_decode(file_get_contents($filename), true);

    $neu = array('titel' => 'bla',
                        'bild' => 'bla',
                        'url' => 'bla',
                        'ID' => 'bla',
                        'bildserver' => 'bla');

    array_push($temparray, $neu);

    file_put_contents("$root/nachrichten/bla.json", json_encode($temparray);

Still nothing (ignore this text I need to add some or else I can't submit -.-)

1
  • Do you have permission to write to the file? More specifically, does the owner/user of the PHP script have permissions to write to the file? Commented Aug 15, 2014 at 14:49

2 Answers 2

2

Basic PHP: '-quoted strings do NOT interpolate variables:

file_put_contents('$root/nachrichten/bla.json', json_encode($neueartikel);
                  ^^^---

Your code is trying to write your file out to a directory whose name is literally $, r, o, etc...

Use "-quoted strings instead.

Sign up to request clarification or add additional context in comments.

6 Comments

I tryed it this way now: file_put_contents("$root/nachrichten/bla.json", json_encode($neueartikel); and also: file_put_contents($root . "/nachrichten/bla.json", json_encode($neueartikel); but it's still not working :/
well, where is $root being defined, and what's its value?
$root = realpath($_SERVER["DOCUMENT_ROOT"]); it's above the code and I can loop $neueartikel: $filename = "$root/nachrichten/bla.json"; $neueartikel = json_decode(file_get_contents($filename), true); So I guess thats not the problem
the file may be 777, but what about the directory it's in?
It's also 777 I think I have to contact my host?? Until now there were never problems oO
|
0
$data[] = $_POST['data'];

$inp = file_get_contents('results.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData;

answered on Append data to a .JSON file with PHP by "Tim"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.