我想从表单页面上传XML文件。上传后,xml文件的值必须放在我的数组中

时间:2021-10-19 01:43:40

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

下面你可以看到我的formpage,在这个页面你上传了一个xml文件。

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

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. But I want that the array will be fill automatic by the values of the XML File. So if you upload a XML File the values of the file will automatically fill my array. Also you can see that I insert the array into my database.

下面你可以看到我的PHP代码,如果你看看你可以看到现在一切都是手动的。我必须自己填充阵列。但我希望数组将由XML文件的值自动填充。因此,如果您上传XML文件,文件的值将自动填充我的数组。您还可以看到我将数组插入到我的数据库中。

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

1 个解决方案

#1


0  

You can use the PHP SimpleXML extension to read XML files and convert them to objects. If you run PHP 5.1.2 or later, it is enabled by default. The PHP manual provides some excellent examples.

您可以使用PHP SimpleXML扩展来读取XML文件并将其转换为对象。如果运行PHP 5.1.2或更高版本,则默认启用它。 PHP手册提供了一些很好的例子。

Step 1: saving the XML file

第1步:保存XML文件

The first thing to decide is whether you want to store the XML files. In that case, have a look at the "POST method upload" example (http://www.php.net/manual/en/features.file-upload.post-method.php). If you want to move the uploaded XML file to a directory named "xml" inside your current folder, it would look like this:

首先要决定是否要存储XML文件。在这种情况下,请查看“POST方法上传”示例(http://www.php.net/manual/en/features.file-upload.post-method.php)。如果要将上载的XML文件移动到当前文件夹中名为“xml”的目录,它将如下所示:

if(!empty($_FILES)) {
    $uploaddir = dirname(__FILE__) . '/xml/';
    $uploadfile = $uploaddir . basename($_FILES['file']['name']);

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        echo 'The XML file has been successfully uploaded.';
    } else {
        echo 'Error occurred while saving the uploaded file.';
    }
}

Step 2: reading the XML file

第2步:读取XML文件

The next step is to load the XML file using SimpleXML and convert it to an object (see examples at http://www.php.net/manual/en/simplexml.examples-basic.php). First you need to define an XML structure for your data. For example:

下一步是使用SimpleXML加载XML文件并将其转换为对象(请参阅http://www.php.net/manual/en/simplexml.examples-basic.php中的示例)。首先,您需要为数据定义XML结构。例如:

<?xml version='1.0' standalone='yes'?>
<boms>
    <bom>
            <aantal>1</aantal>
            <manufactorer>Panasonic</manufactorer>
            <partno>EEEFC1H1R0R</partno>
            <description>Capacitor 0603</description>
            <footprint>CAP0603</footprint>
            <refdes>B1</refdes>
    </bom>
    <bom>
            <aantal>2</aantal>
            <manufactorer>Vishay</manufactorer>
            <partno>MAL215371228E3</partno>
            <description>Capacitor 1210</description>
            <footprint>CAP1210</footprint>
            <refdes>C6,C7</refdes>
    </bom>
</boms>

Then to read the XML file and loop through the rows to echo them or save them to the database:

然后读取XML文件并遍历行以回显它们或将它们保存到数据库:

if(isset($uploadfile) && file_exists($uploadfile)) {
    $bom = simplexml_load_file($uploadfile);
    if(!$bom) {
        die('Unable to load XML file.');
    }

    foreach($bom as $row) {
        echo "<tr>
        <td>" . $row->aantal . "</td>
        <td>" . $row->manufactorer . "</td>
        <td>" . $row->partno . "</td>
        <td>" . $row->description . "</td>
        <td>" . $row->footprint . "</td>
        <td>" . $row->refdes . "</td>
      </tr>";
    }
}

A few notes on these examples:

关于这些例子的几点说明:

  • There is no validation as to whether the uploaded file actually is an XML file and if it has the structure you defined. Make sure you check that before parsing it.
  • 没有验证上传的文件是否实际上是XML文件,以及它是否具有您定义的结构。确保在解析之前检查它。

  • The "xml" directory has to be writable by your webserver.
  • “xml”目录必须可由您的Web服务器写入。

Hope this helps.

希望这可以帮助。

#1


0  

You can use the PHP SimpleXML extension to read XML files and convert them to objects. If you run PHP 5.1.2 or later, it is enabled by default. The PHP manual provides some excellent examples.

您可以使用PHP SimpleXML扩展来读取XML文件并将其转换为对象。如果运行PHP 5.1.2或更高版本,则默认启用它。 PHP手册提供了一些很好的例子。

Step 1: saving the XML file

第1步:保存XML文件

The first thing to decide is whether you want to store the XML files. In that case, have a look at the "POST method upload" example (http://www.php.net/manual/en/features.file-upload.post-method.php). If you want to move the uploaded XML file to a directory named "xml" inside your current folder, it would look like this:

首先要决定是否要存储XML文件。在这种情况下,请查看“POST方法上传”示例(http://www.php.net/manual/en/features.file-upload.post-method.php)。如果要将上载的XML文件移动到当前文件夹中名为“xml”的目录,它将如下所示:

if(!empty($_FILES)) {
    $uploaddir = dirname(__FILE__) . '/xml/';
    $uploadfile = $uploaddir . basename($_FILES['file']['name']);

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        echo 'The XML file has been successfully uploaded.';
    } else {
        echo 'Error occurred while saving the uploaded file.';
    }
}

Step 2: reading the XML file

第2步:读取XML文件

The next step is to load the XML file using SimpleXML and convert it to an object (see examples at http://www.php.net/manual/en/simplexml.examples-basic.php). First you need to define an XML structure for your data. For example:

下一步是使用SimpleXML加载XML文件并将其转换为对象(请参阅http://www.php.net/manual/en/simplexml.examples-basic.php中的示例)。首先,您需要为数据定义XML结构。例如:

<?xml version='1.0' standalone='yes'?>
<boms>
    <bom>
            <aantal>1</aantal>
            <manufactorer>Panasonic</manufactorer>
            <partno>EEEFC1H1R0R</partno>
            <description>Capacitor 0603</description>
            <footprint>CAP0603</footprint>
            <refdes>B1</refdes>
    </bom>
    <bom>
            <aantal>2</aantal>
            <manufactorer>Vishay</manufactorer>
            <partno>MAL215371228E3</partno>
            <description>Capacitor 1210</description>
            <footprint>CAP1210</footprint>
            <refdes>C6,C7</refdes>
    </bom>
</boms>

Then to read the XML file and loop through the rows to echo them or save them to the database:

然后读取XML文件并遍历行以回显它们或将它们保存到数据库:

if(isset($uploadfile) && file_exists($uploadfile)) {
    $bom = simplexml_load_file($uploadfile);
    if(!$bom) {
        die('Unable to load XML file.');
    }

    foreach($bom as $row) {
        echo "<tr>
        <td>" . $row->aantal . "</td>
        <td>" . $row->manufactorer . "</td>
        <td>" . $row->partno . "</td>
        <td>" . $row->description . "</td>
        <td>" . $row->footprint . "</td>
        <td>" . $row->refdes . "</td>
      </tr>";
    }
}

A few notes on these examples:

关于这些例子的几点说明:

  • There is no validation as to whether the uploaded file actually is an XML file and if it has the structure you defined. Make sure you check that before parsing it.
  • 没有验证上传的文件是否实际上是XML文件,以及它是否具有您定义的结构。确保在解析之前检查它。

  • The "xml" directory has to be writable by your webserver.
  • “xml”目录必须可由您的Web服务器写入。

Hope this helps.

希望这可以帮助。