dongshadu4498 2015-03-09 02:11
浏览 68

上传xml文件,解析它并使用PHP(MySQLi)将节点值存储在MySQL数据库中

I've spent quite some time searching for this but I couldn't find any that works.

I uploaded xml file & I want to load it so that I can parse it & store the node values into my database. I managed to upload the file (the uploaded file was in the "uploads" folder). But I couldn't parse the uploaded xml file & extract data from the specific element tag to store them in the database.

The $myfileLink in the code below works when I paste the link into the browser. $myfileLink in this case is "http://localhost/aps/uploads/user.xml"

Here's my php script:

    <?php

    error_reporting(E_ALL); ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

    include 'conn.php';

    $query = "INSERT INTO report (File_Name, File_Size, File_Link, File_Type, Created_Timestamp, Name, Address, Phone, Fax) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $ds = "/";
    $storeFolder = 'uploads';


    if(!empty($_FILES)) {
        $myfileName = basename($_FILES['file']['name']);
        $myfileType = $_FILES['file']['type'];
        $myfileSize = $_FILES['file']['size'];
        $myfileLink = 'http://localhost/aps/uploads/' . rawurlencode($myfileName);
        $myfileDate = date("Y-m-d H:i:s");

        $tempFile = $_FILES['file']['tmp_name'];
        $targetPath = dirname(__FILE__) . $ds . $storeFolder . $ds;
        $targetFile = $targetPath . $_FILES['file']['name'];
        move_uploaded_file($tempFile, $targetFile);

        $xml = simplexml_load_file($myfileLink) or die("Error: Cannot create object");

        $myname = $xml->name;
        $myaddress = $xml->address;
        $myphone = $xml->phone;
        $myfax = $xml->fax;

        $stmt = $conn->prepare($query);
        $stmt->bind_param('sisssssss', $myfileName, $myfileSize, $myfileLink, $myfileType, $myfileDate, $myname, $myaddress, $myphone, $myfax);

        if($stmt->execute()) {
            echo 'Success!';
        } else {
            die('Error: ('. $conn->errno . ') ' . $conn->error);
        }

    }

    ?>

Here's example of user.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<user>
    <name>Example 1</name>
    <address>Example 2</address>
    <phone>Example 3</phone>
    <fax>Example 4</fax>
</user>

I've also tried this code from php manual to check whether the file can load or not. But it gave me error "Failed to open file".

<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('http://localhost/aps/uploads/user.xml')) {
    $xml = simplexml_load_file('http://localhost/aps/uploads/user.xml');

    print_r($xml);
} else {
    exit('Failed to open file.');
}
?>

What am I doing wrong here? Can anyone help? Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路