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!