打开上传的json文件

时间:2018-10-16 16:06:29

标签: php json upload

我正在尝试完成一个上传工具,该工具可以打开json文件并将其显示在表格中。我在下面发布的脚本(basis.php)已经正常运行。

basis.php     

$data = file_get_contents("data.json"); // put the contents of the file into a variable

$characters = json_decode($data); // decode the JSON feed
?>
 <!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="css/pure-min.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<table class="pure-table">
    <thead>
        <tr>
            <th>Tijdstempel</th>
            <th>Voornaam</th>
            <th >NHL E-mailadres</th>
            <th>Geboortejaar</th>
            <th>Kwaliteit</th>
            <th>Moeilijkheidsniveau</th>
            <th>Gegevenscontrole</th>
        </tr>
    </thead>
        <?php foreach ($characters as $character) : ?>
    <tbody>   
        <tr >
            <td> <?php echo $character->Tijdstempel; ?> </td>
            <td> <?php echo $character->Voornaam; ?> </td>
            <td> <?php echo $character->Email; ?> </td>
            <td> <?php echo $character->Geboortejaar; ?> </td>
            <td> <?php echo $character->Kwaliteit; ?> </td>
            <td> <?php echo $character->Moeilijkheidsniveau; ?> </td>
            <td> <?php echo $character->Gegevenscontrole; ?> </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
</body>
</html> 

现在问题来了,我试图更改base.php并将其适应以下index.php。这样,我希望能够上传一个合适的json文件。

index.php

<!doctype html>
<html lang="nl">
<head>
    <meta charset=utf-8>
    <title>Basisformulier voor het uploaden van een bestand</title>
</head>
<body>

    <h1>Excel bestanden importeren en exporteren</h1>

    <form action="basis.php" method="post" enctype="multipart/form-data">
        <label for="frm_importfile">Selecteer het bestand:</label>

        <input type="file" name="importfile" id="frm_importfile">
        <button type="submit">Upload het bestand</button>
    </form>
</body>

所以我尝试通过这种方式更改base.php:

basis.php(不起作用)

<?php
if(!isset($_FILES['importfile'])){
        echo "FOUT: Je hebt geen bestand geselecteerd om te uploaden";
        exit;
    }

$data = fopen($_FILES['importfile']['tmp_name'], "r");


$characters = json_decode($data); // decode the JSON feed
?>
 <!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="css/pure-min.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<table class="pure-table">
    <thead>
        <tr>
            <th>Tijdstempel</th>
            <th>Voornaam</th>
            <th >NHL E-mailadres</th>
            <th>Geboortejaar</th>
            <th>Kwaliteit</th>
            <th>Moeilijkheidsniveau</th>
            <th>Gegevenscontrole</th>
        </tr>
    </thead>
        <?php foreach ($characters as $character) : ?>
    <tbody>   
        <tr >
            <td> <?php echo $character->Tijdstempel; ?> </td>
            <td> <?php echo $character->Voornaam; ?> </td>
            <td> <?php echo $character->Email; ?> </td>
            <td> <?php echo $character->Geboortejaar; ?> </td>
            <td> <?php echo $character->Kwaliteit; ?> </td>
            <td> <?php echo $character->Moeilijkheidsniveau; ?> </td>
            <td> <?php echo $character->Gegevenscontrole; ?> </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
</body>
</html> 

我已经在互联网上寻找解决方案很长时间了,但仍然无法解决问题。我希望有人能帮助我,并向我展示如何将json文件上传到base.php。

问候,

塞拉

2 个答案:

答案 0 :(得分:1)

您打开了文件,但没有从文件中读取任何数据。
一种获取文件内容的简单方法是file_get_contents

$data = file_get_contents($_FILES['importfile']['tmp_name']);

答案 1 :(得分:0)

您尝试过

$ data = file_get_contents($ _ FILES ['importfile'] ['tmp_name']);