如何使用PHP将JSON数据记录到文件?

时间:2023-01-27 10:53:08

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.

但这绝对不是正确的方法。

2 个解决方案

#1


3  

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

如果$ _POST数组包含您需要的所有数据,则可以将其编码为JSON并写入文件:

<?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.

如果你想要附加到文件,你需要首先将文件读入一个数组,添加数组的新部分然后再写入它再写回文件,就像我的朋友@ Rizier123描述的那样。

#2


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.

无需将整个JSON文件解码为数组。

#1


3  

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

如果$ _POST数组包含您需要的所有数据,则可以将其编码为JSON并写入文件:

<?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.

如果你想要附加到文件,你需要首先将文件读入一个数组,添加数组的新部分然后再写入它再写回文件,就像我的朋友@ Rizier123描述的那样。

#2


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.

无需将整个JSON文件解码为数组。