3

This is the code I've figured out.

<?php
$username = $_POST['username'];
$email = $_POST['email'];
$json = '{"username":"'.$username.'",'.'"email":"'.$email.'"}';
$file = fopen('token_data.json','w+');
fwrite($file, $json);
fclose($file);
?>

But this is absolutely not the right way.

5
  • 1) Put the data in an array and use json_encode() to encode it to JSON 2) When you want to add stuff, take the file content, decode it and add your array to it, and then encode it before you save it again. Commented Jun 18, 2016 at 1:45
  • What makes you think so? How would you improve it? Hint: Have a look at json_encode Commented Jun 18, 2016 at 1:45
  • What errors are you getting? Commented Jun 18, 2016 at 1:45
  • Since $_POST is an array you can go straight to JSON with it. Commented Jun 18, 2016 at 1:47
  • @JeffPuckettII I'm not getting errors, by this way I can't add data to the file, I'm just overwriting it. Commented Jun 18, 2016 at 1:49

2 Answers 2

2

If your $_POST array has all of the data you need you can encode it as JSON and write to a file:

<?php

    $json = json_encode($_POST);
    $file = fopen('token_data.json','w+');
    fwrite($file, $json);
    fclose($file);
?>

If you want to append to the file you will need to read the file into an array first, add the newer parts of the array then encode it again before writing back to the file just like my friend @Rizier123 describes.

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

3 Comments

Just as @Rizier123 and I described it. Did you want me to write the code for you?
Please write it, I don't quite understand reading the file into an array...
I knew I shouldn't have asked @skygate but that is just not how Stack Overflow works. We're here to help you solve issues, not write the code for you. I have given you the steps. Read the file with something like fopen(). Decode the JSON into an array with something like json_decode(). Add new data to the array, maybe array_push(). Give that a shot and come back when and if you need help with the code you have written.
1

Okay, I found a more efficient way to do this.

Original Answer

// read the file if present
$handle = @fopen($filename, 'r+');

// create the file if needed
if ($handle === null)
{
    $handle = fopen($filename, 'w+');
}

if ($handle)
{
    // seek to the end
    fseek($handle, 0, SEEK_END);

    // are we at the end of is the file empty
    if (ftell($handle) > 0)
    {
        // move back a byte
        fseek($handle, -1, SEEK_END);

        // add the trailing comma
        fwrite($handle, ',', 1);

        // add the new json string
        fwrite($handle, json_encode($event) . ']');
    }
    else
    {
        // write the first event inside an array
        fwrite($handle, json_encode(array($event)));
    }

        // close the handle on the file
        fclose($handle);
}

Without decoding the whole JSON file into the arrays.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.