【发布时间】:2020-09-14 22:04:12
【问题描述】:
我正在尝试使用 simplexml_load_file 上传 xml 文件,但是当文件中是特殊字符时,我收到一个错误(没有特殊字符,一切正常)。 首先,我正在使用来自另一个程序的 XML 文件,所以我无法更改它。
这是 xml 示例:
<wuo>
<header>
<title>title1</title>
</header>
<body>
<lp>1</lp>
<sign>124.455</sign>
<text>sample text with & character</text> //<-- this is causing the problem
</body>
<body>
<lp>2</lp>
<sign>12556.455</sign>
<text>sample text 2</text>
</body>
</wuo>
我的代码:
if(isset($_POST["submit"]))
{
if(isset($_FILES['wuo']) && ($_FILES['wuo']['error'] == UPLOAD_ERR_OK))
{
if(!simplexml_load_file($_FILES['wuo']['tmp_name']))
{
// if file has special character in it this fires up
echo 'Error';
}
else
$file = simplexml_load_file($_FILES['wuo']['tmp_name']);
print_r($file);
/**
showing file to html etc... unimportant code for this case
**/
}
else
echo 'Error:' . $_FILES['wuo']['error'];
}
我知道,我应该在simplexml_load_file 之前做点什么,但我不知道具体是什么。或者也许我应该使用其他东西......
顺便说一句:我不需要保护它,因为它只适合我。
【问题讨论】:
标签: php xml file-upload