首页 文章

JSON 文件未更新到服务器

提问于
浏览
1

我无法使用以下代码将 JSON 文件写入服务器:

<form method="POST">
<a href="index.php">Back</a>
<p>
    <label for="id">ID</label>
    <input type="text" id="id" name="id">
</p>
<p>
    <label for="firstname">Firstname</label>
    <input type="text" id="firstname" name="firstname">
</p>
<p>
    <label for="lastname">Lastname</label>
    <input type="text" id="lastname" name="lastname">
</p>
<p>
    <label for="address">Address</label>
    <input type="text" id="address" name="address">
</p>
<p>
    <label for="gender">Gender</label>
    <input type="text" id="gender" name="gender">
</p>
<input type="submit" name="save" value="Save">
</form>

<?php
if(isset($_POST['save'])){
    //open the json file
    $data = file_get_contents('members.json');
    $data = json_decode($data);

    //data in out POST
    $input = array(
        'id' => $_POST['id'],
        'firstname' => $_POST['firstname'],
        'lastname' => $_POST['lastname'],
        'address' => $_POST['address'],
        'gender' => $_POST['gender']
    );

    //append the input to our array
    $data[] = $input;
    //encode back to json
    $data = json_encode($data, JSON_PRETTY_PRINT);
    file_put_contents('members.json', $data);

    header('location: index.php');
}
?>

该代码在 localhost 中完美运行。我已将权限设置为所有必需的目录。 Linux Server 安装了 PHP 5.4.16 版本。

请让我知道上述问题的任何解决方案。

2 回答

  • 0

    我更新了你的代码并一路评论。请运行并告诉我您的代码说明了什么。

    更新

    if(isset($_POST['save'])){
    
        //Check to see if file exist.
        echo file_exists('members.json') ? 'Found it' : 'What file';
    
        //open the json file
        $data = file_get_contents('members.json');
    
        $data = json_decode($data, TRUE); //<-- This was being outputted as an object.  I added the
        //TRUE flag to output as an array.
    
        //Lets test for some json errors.
        echo 'Decode json errors: ' . json_last_error();
    
        //data in out POST
        //Since your $data is an array now, you can add another array to your $data array.
        $input = array(
            'id' => $_POST['id'],
            'firstname' => $_POST['firstname'],
            'lastname' => $_POST['lastname'],
            'address' => $_POST['address'],
            'gender' => $_POST['gender']
        );
    
        //append the input to our array
        $data[] = $input;
        //encode back to json
        $data = json_encode($data); //Do you really need the json pretty print?  Out it goes.
    
        //For testing purposes run this line and report back what it says.
        echo 'Encode json errors: ' . json_last_error();
    
        //It looks like the file is not writable.  So we are going to change the permissions.
        if(!is_writable('members.json')){
    
          chmod('members.json', 0777); //This pretty much gives full read-write permissions to that file.
    
        }
    
        //For giggles lets test to see if you have your permissions set up to write.
        echo is_writable('members.json') ? 'Yup' : 'No way';
    
        file_put_contents('members.json', $data);
    
        //We are going to change the permissions back to readonly.
        //You should read up on permissions and how to properly set them.
        chmod('members.json', 0644); //This restricts to read only.
    
    }
    
  • 0

    我在搜索时得到了答案,如下所示我以 root 身份更新了 Login 并运行以下命令

    chcon -t public_content_rw_t /var/www
    setsebool -P allow_smbd_anon_write 1
    setsebool -P allow_httpd_anon_write 1
    

    https://serverfault.com/questions/131105/

相关问题