我花了很长时间寻找这个,但我找不到任何有用的东西。
我上传了xml文件&我想加载它以便我可以解析它&将节点值存储到我的数据库中。我设法上传了文件(上传的文件位于"上传"文件夹中)。但我无法解析上传的xml文件&从特定元素标记中提取数据以将其存储在数据库中。
将链接粘贴到浏览器中时,下面代码中的$myfileLink
有效。
在这种情况下,$myfileLink
是" http://localhost/aps/uploads/user.xml
"
这是我的PHP脚本:
<?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);
}
}
?>
以下是user.xml文件的示例:
<?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>
我也尝试过php手册中的这段代码来检查文件是否可以加载。但它给了我错误&#34;无法打开文件&#34;。
<?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.');
}
?>
我在这里做错了什么?有人可以帮忙吗?谢谢!