如何在不上传文件的情况下使用PHP从HTML表单读取XML文件的内容?这是我的表格:
<form action="readxml.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
我想使用SimpleXML处理用户选择的XML,将其值存储在数据库中,因此我实际上不需要上传文件。
谢谢!
如果文件不在服务器上,就无法从PHP实现这一点。您必须上传文件,或者您可以在客户端处理文件,而无需通过以下方式之一上传:
- 使用HTML5 FileReader API,仅适用于Chrome、FF和IE 10+
- 使用ActiveX在客户端处理文件,除非在IE上,否则无法工作
- 使用SilverLight在客户端处理文件
- 用户ActionScript在客户端处理该文件
然而,我建议将文件上传到一个临时位置,然后处理它,然后删除它。它更简单,可以像在服务器上一样在任何地方工作。
以下是上传文件的方法:PHP文件上传
以下是删除文件的方法:PHP unlink()函数
以下是读取文件的方法:PHP文件打开/读取/关闭
你不能。我认为即使在客户端,用javascript解析文件也是不可行的。但我可能错了。您可以将其上传到服务器上,将其存储在临时文件夹中,然后将其删除。
这在我的html.php文件中。。。我只是设置它提交给自己,因为这对我来说很容易。
<?php
if (isset($_FILES['file'])) {
$file = $_FILES['file']['tmp_name'];
$catalog = simplexml_load_file($file);
echo '<table style="border-spacing: 10px;">';
echo '<tr><th>Title</th><th>Author</th></tr>';
foreach ($catalog->book as $b) {
echo '<tr><td>'.$b->title.'</td><td>'.$b->author.'</td></tr>';
}
echo '</table>';
}
else {
?>
<!-- change the filename below -->
<form action="filename.html" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php } ?>
这是我在一个文件中使用的xml,用表单…上传
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
它只是显示上传表格或图书表
Title Author
XML Developer's Guide Gambardella, Matthew
Midnight Rain Ralls, Kim
Maeve Ascendant Corets, Eva
还有。。。如以下链接所述。。。如果不移动或重命名临时文件,php脚本结束时它将被删除。php::tmp文件要保存多久?