我想上传一个文件,当文件上传时,文件的值必须放在我的数组中 [英] I want to upload a file and when the file upload, the values of the file has to be put in my array

查看:122
本文介绍了我想上传一个文件,当文件上传时,文件的值必须放在我的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面您可以看到我的表单页,在此页面上传一个xml文件。

Below you can see my formpage, at this page you upload a xml file.

<html>
<body>

<form enctype="multipart/form-data" action="arrayincludefiletest.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<table width="600">
<tr>
<td>File name:</td>
<td><input type="file" name="file" /></td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>

</body>
</html>

下面你可以看到我的PHP代码,如果你看看你可以看到, 。我必须自己填补数组。您还可以看到我将数组插入我的数据库。我想上传一个xml文件,并将xml文件的值自动放入我的数组中。

Below you can see my PHP code, if you take a look you can see that now everything is manual. I have to fill the array by myself. You can also see that I insert the array into my database. I want to upload a xml file and that the values of the xml file automatic be put in my array.

<html>
<head>
<title> Bom Array </title>
</head>
<body>

<?php

$bom= array(
        array("Aantal" =>1, "Manufactorer" =>"Panasonic", "Partno" =>"EEEFC1H1R0R", 
                "Description" =>"Capacitor 0603","Footprint" =>"CAP0603", "Refdes" =>"B1"),
        array("Aantal" =>2, "Manufactorer" =>"Vishay", "Partno" =>"MAL215371228E3", 
                "Description" =>"Capacitor 1210","Footprint" =>"CAP1210", "Refdes" =>"C6,C7"),
        array("Aantal" =>3, "Manufactorer" =>"Ferroxcube", "Partno" =>"MAL215371109E3", 
                "Description" =>"Buzzer 80dB 3,4 KHz","Footprint" =>"KPEG238", "Refdes" =>"C8,C25"),
        array("Aantal" =>4, "Manufactorer" =>"Philips", "Partno" =>"EEEFC1E101P", 
                "Description" =>"Tantaal 100uF, 6,3V Case_B","Footprint" =>"Case_B", "Refdes" =>"C1")
);

echo "<table border='1'>";
echo "<tr>
        <th>Aantal</th>
        <th>Manufactorer</th>
        <th>Partno</th>
        <th>Description</th>
        <th>Footprint</th>
        <th>Refdes</th>
     </tr>";

echo "<tr>
        <td>" . $bom[0]['Aantal'] . "</td>
        <td>" . $bom[0]['Manufactorer'] . "</td>
        <td>" . $bom[0]['Partno'] . "</td>
        <td>" . $bom[0]['Description'] . "</td>
        <td>" . $bom[0]['Footprint'] . "</td>
        <td>" . $bom[0]['Refdes'] . "</td>
      </tr>";

echo "<tr>
        <td>" . $bom[1]['Aantal'] . "</td>
        <td>" . $bom[1]['Manufactorer'] . "</td>
        <td>" . $bom[1]['Partno'] . "</td>
        <td>" . $bom[1]['Description'] . "</td>
        <td>" . $bom[1]['Footprint'] . "</td>
        <td>" . $bom[1]['Refdes'] . "</td>
      </tr>";

echo "<tr>
        <td>" . $bom[2]['Aantal'] . "</td>
        <td>" . $bom[2]['Manufactorer'] . "</td>
        <td>" . $bom[2]['Partno'] . "</td>
        <td>" . $bom[2]['Description'] . "</td>
        <td>" . $bom[2]['Footprint'] . "</td>
        <td>" . $bom[2]['Refdes'] . "</td>
      </tr>";

echo "<tr>
        <td>" . $bom[3]['Aantal'] . "</td>
        <td>" . $bom[3]['Manufactorer'] . "</td>
        <td>" . $bom[3]['Partno'] . "</td>
        <td>" . $bom[3]['Description'] . "</td>
        <td>" . $bom[3]['Footprint'] . "</td>
        <td>" . $bom[3]['Refdes'] . "</td>
      </tr>";
echo "</table>";

// Connectie database and Insert into database
$con = mysqli_connect("localhost", "csa", "csa", "csa");

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL:" . mysqli_connect_error();
}

if(is_array($bom))
{
    $sql= "INSERT INTO bom (Aantal, Manufactorer, Partno, Description, Footprint, Refdes) values";

    $valuesArr = array();
    foreach ($bom as $row)
    {   
        $aantal = (int) $row['Aantal'];
        $manufactorer = mysqli_real_escape_string($con, $row['Manufactorer']);
        $partno = mysqli_real_escape_string($con, $row['Partno']);
        $description = mysqli_real_escape_string($con, $row['Description']);
        $footprint = mysqli_real_escape_string($con, $row['Footprint']);
        $refdes = mysqli_real_escape_string($con, $row['Refdes']);

        $valuesArr[] = "('$aantal', '$manufactorer', '$partno', '$description', '$footprint', '$refdes')";
    }

    $sql .= implode(',', $valuesArr);
    mysqli_query($con, $sql) or die('Error:' . mysqli_errno($con));

}

mysqli_close($con);

?>

</body>
</html>


推荐答案

使用 xml_parse_into_struct / code>函数。

Use xml_parse_into_struct() function for this.

$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();

$fp = fopen($xmlfile, 'r');
$xmldata = fread($fp, 4096);

xml_parse_into_struct($xmlparser,$xmldata,$values);

xml_parser_free($xmlparser);
print_r($values);

这篇关于我想上传一个文件,当文件上传时,文件的值必须放在我的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆