【发布时间】:2016-02-09 12:26:42
【问题描述】:
我正在尝试使用用户输入的数据将其存储到 XML 数据库文件中以供以后使用。但是,由于某种原因,我继续收到此错误。
问题代码总结如下,整个PHP文件在上下文下面。
我尝试使用strval() 将我得到的整数(我在使用uniqid() 之前使用mt_rand())转换为(string),甚至尝试将Type Casting 转换为(object)。都没有运气。 uniqid() 已经返回一个字符串。事实上,当我尝试对对象(来自uniqid() 和mt_rand())进行类型转换时返回的错误是不同的,这让生活变得混乱:
[![错误 2 idGenerated 的输出是一个对象][2]][2]
为什么哦为什么 createTextNode 讨厌字符串、整数和对象?为什么文件中的其他变量(经过 SAME 过程)完全可以通过?
总结问题代码
$idGenerated = uniqid();
$dom = new DOMDocument();
$dom->load('../test.xml');
$idDec = $dom->createTextNode($idGenerated);
$id->appendChild($idDec);
$messageElement->appendChild($id);
整个文件:
<?php
session_start();
//Generate random number to avoid overwrite of file
$randomNumber = mt_rand(10, 99);
move_uploaded_file($_FILES['attachment'][tmp_name], "../uploads/" . $randomNumber . $_FILES['attachment'][name]);
//General Variables
***$idGenerated = uniqid();***
$currentDate = date('l jS \of F h:i:s A');
$sender = $_SESSION['user'];
$message = $_POST['messageText'];
$up_file = $randomNumber . $_FILES['attachment'][name];
$up_file_location = "uploads/" . $up_file;
echo "<br>";
var_dump($currentDate);
echo "<br>";
var_dump($sender);
echo "<br>";
var_dump($message);
echo "<br>";
var_dump($up_file);
echo "<br>";
var_dump($up_file_location);
$dom = new DOMDocument();
$dom->load('../test.xml');
//Dom Variable Declarations
//Declare String Data
***$idDec = $dom->createTextNode($idGenerated);***
$date = $dom->createTextNode($currentDate);
$name = $dom->createTextNode($sender);
$messageText = $dom->createTextNode($message);
$link = $dom->createTextNode($up_file_location);
$linkName = $dom->createTextNode($up_file);
//Declare Data Elements
$id = $dom->createElement('id');
$timecode = $dom->createElement('timecode');
$author = $dom->createElement('author');
$content = $dom->createElement('content');
$filePath = $dom->createElement('filePath');
$fileName = $dom->createElement('fileName');
$messageElement = $dom->createElement('message');
//Create XML Data Structure
//ID
***$id->appendChild($idDec);***
//Date
$timecode->appendChild($date);
//Name
$author->appendChild($name);
//Message
$content->appendChild($messageText);
//File (if there is one!)
$filePath->appendChild($link);
$fileName->appendChild($linkName);
//Message Wrapper Element
***$messageElement->appendChild($id);***
$messageElement->appendChild($timecode);
$messageElement->appendChild($author);
$messageElement->appendChild($content);
$messageElement->appendChild($filePath);
$messageElement->appendChild($fileName);
//Load last existing XML entry for reference (which, since they are all in reverse order, will actually be the FIRST entry in the database)
$xml = simplexml_load_file('../test.xml');
$lastEntry = $xml->log->message[0];
//Place new data BEFORE the last existing message Element
$newEntry = $dom->firstChild->appendChild($messageElement);
$lastEntry->parentNode->insertBefore($newEntry, $lastEntry);
$dom->save('../test.xml');
header("Location: ../index.php");
?>
【问题讨论】:
-
你在哪里定义
$messageElementvar? -
顺便说一下,您应该真的在允许上传之前检查文件类型。 mt150003.students.fhstp.ac.at/guestbook/uploads/86hack.php
-
哎呀...我肯定会向那个学习...感谢您的提醒。