我试图允许用户上传一个文件,然后使用 SimpleXML 将其内容读入数据库。我的代码如下:
else
{
$info = pathinfo($_FILES['xml_file']['tmp_name']);
$extension = $info['extension'];
if($extension != 'xml')
{
flash_message($lang->wiki_invalid_file, 'error');
admin_redirect('index.php?module=wiki-import');
}
else
{
if($_FILES['xml_file']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['xml_file']['tmp_name']))
{
$string = file_get_contents($_FILES['xml_file']['tmp_name']);
$xml = new SimpleXMLElement($string);
foreach($xml->article as $article)
{
$query = "INSERT INTO " . TABLE_PREFIX . "wiki('authors','title','content','protected','lastauthor','lastauthorid','category') VALUES('" . $article->authors . "','" . $article->title . "','" . $article->content . "','" . $article->protected . "','" . $article->lastauthor . "','" . $article->lastauthorid . "','" . $article->category . "',)";
$sql = $db->write_query($query);
if($db->error_number() > 0)
{
flash_message($lang->wiki_import_error, 'error');
admin_redirect('index.php?module=wiki-import');
}
elseif(!$sql)
{
flash_message($lang->wiki_import_error, 'error');
admin_redirect('index.php?module=wiki-import');
}
else
{
flash_message($lang->wiki_import_success, 'success');
admin_redirect('index.php?module=wiki-import');
}
}
}
else
{
flash_message($lang->wiki_import_error, 'error');
admin_redirect('index.php?module=wiki-import');
}
}
}
(我正在使用 MyBB,它有 和 的定义flash_message
,admin_redirect
它将用户重定向到另一个页面并显示一条消息。)
我的问题是我似乎根本无法上传它,没有显示错误消息。此外,页面只是刷新。一个示例内容是:
<?xml version="1.0" ?>
<wiki>
<article>
<id>1</id>
<title>To be Exported</title>
<content>
Export me!
</content>
<category>Meta</category>
<lastauthor>admin</lastauthor>
<lastauthorid>1</lastauthorid>
<protected>0</protected>
<authors>1</authors>
</article>
</wiki>
文件上传框的id
andname
属性是'xml_file'。我的本地主机上也有 PHP 版本 5.3.13。