2013-10-01 58 views
0

下面你可以看到我的腳本,在這個頁面你上傳一個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> 

下面你可以看到我的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> 

回答

0

使用xml_parse_into_struct()函數爲此。

$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); 
+0

我必須把這段代碼放在哪裏,你看到我的代碼如上。我必須把它放在哪裏? –

+0

文件上傳完成後,您會知道它已放置在哪裏以及獲取了哪個名稱。因此,請使用上傳文件的路徑和名稱更改$ xmlfile變量,並在上傳後運行它。 –

+0

詢問代碼的問題必須證明對所解決問題的最小理解。包括嘗試解決方案,爲什麼他們沒有工作,以及預期的結果。另請參閱:http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist –

相關問題