如何使用 PHP 写入 nginx 网络服务器上的 json 文件?

How can I write to a json file on a nginx web server using PHP?

我正在尝试创建一个网页,用户可以在其中输入用户名和 ID,并使用提供的输入更新 .json 文件,但每当我按下提交按钮时,它都会将我重定向到空白页(即示例。com/index。php),它不会更新 JSON 文件。

我试过:

我在 Raspberry Pi 3B+

上使用 nginx 版本 1.18.0

index.html、index.php 和 whitelist.json 都在同一目录中。

HTML代码(index.html):

<form method="post" action="index.php">
        
<div class="group">      
    <input type="text" name="username" required>
    <span class="highlight"></span>
    <span class="bar"></span>
    <label>Username</label>
</div>
            
<div class="group">      
    <input type="text" name="uuid" required>
    <span class="highlight"></span>
    <span class="bar"></span>
    <label>UUID</label>
</div>

<button id="btn"> <p id="btnText">Submit</p> </button>

</form>

PHP代码(index.php):

<?php
$data = file_get_contents('whitelist.json');

$json_arr = json_decode($data, true);

$json_arr[] = array('uuid'=>$_POST['uuid'], 'name'=>>$_POST['username']);

file_put_contents('whitelist.json', json_encode($json_arr));
?>

首先,这个'name'=>>$_POST['username']应该是'name'=>$_POST['username'](你多了一个>

其次,我会将 index.php 文件重命名为 submit.php 之类的其他名称,这样就不会发生冲突(不要忘记也将 action="index.php" 更改为新文件名)

如果你做了这两件事,whitelist.json 应该用输入的值更新(但你仍然会遇到我和@Barmar 在评论中描述的空白页问题)


OR 而不是为 HTML 和 PHP 使用两个单独的索引,您可以只使用一个文件 index.php,用于两者(我推荐的);然后您可以在同一目录中创建 whitelist.json

index.php 示例:

<?php
    # check if submit button was pressed and make sure both textboxes aren't empty
    if(isset($_POST['submit']) && !empty($_POST['uuid']) && !empty($_POST['username'])){
        
        # declare file path & name for .json file 
        $filepath = "whitelist.json";

        # check if it exists 
        if(file_exists($filepath)){
            # if it does, get contents and decode into array
            $data = file_get_contents($filepath);
            $json_arr = json_decode($data, true);
        }

        # add to existing array from above or create a new one with submitted data
        $json_arr[] = array('uuid' => $_POST['uuid'], 'name' => $_POST['username']);

        # open or create file & truncate it.
        $fopen = fopen($filepath, "w");

        # try to write data to said file; return $output regarding result
        if($b = fwrite($fopen, json_encode($json_arr, JSON_PRETTY_PRINT))) $output = "Success! ${b} bytes written to: ${filepath}";
        else $output = "Error!";

        # close open file/handle
        fclose($fopen);
    }
?>

<html>
    <head>
        <title>Create ID</title>
    </head>
    <body>
        <h2>Create ID</h2>
        <form action="" method="POST" enctype="multipart/form-data">
            <input required type="text" name="uuid" placeholder="UUID"><br>
            <input required type="text" name="username" placeholder="Username">
            <input type="submit" name="submit" value="Submit">
        </form>

        <!-- echo $output only if declared and different than NULL -->
        <span><?php if(isset($output)) echo $output; ?></span>
    </body>
</html>
<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
} else { 
    echo "<p>Welcome to PHP</p>";
}
?>