-1

How can I read the content of an XML file from an HTML form with PHP without uploading the file? This is my form:

<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>

I want to process the XML that the user selects using SimpleXML to store its values in a database, thus I don't need to actually upload the file.

Thanks!

Alonso Arellano
  • 459
  • 2
  • 5
  • 8

3 Answers3

4

You cannot do that from PHP without the file actually being on the server. You must upload the file or you can process it on the client side without uploading by one of the following:

  • Use HTML5 FileReader API, only works on Chrome, FF, and IE 10+
  • Use ActiveX to process the file at the client side, will not work except on IE.
  • Use SilverLight to process the file at the client side.
  • User ActionScript to process the file at the client side.

However I recommend uploading the file in a temp location and process it then delete it. It is simpler and will work everywhere as it is on the server.

This is how to upload a file: PHP File Upload

This is how to delete a file: PHP unlink() Function

This is how to read a file: PHP File Open/Read/Close

Amr Shawqy
  • 106
  • 5
0

You can't. I think it's not doable even on the client side by parsing the file with javascript. But I might be wrong. You can upload it on the server, store it in a temporary folder then deleting it.

just some guy
  • 524
  • 1
  • 4
  • 18
-1

this is in my html/php file... I just set it up to submit to itself because that was easy for me.

<?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 } ?>

and here is the xml that I used in a file to upload with the form...

<?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> 

it just either displays the upload form or the table of books

Title                      Author
XML Developer's Guide      Gambardella, Matthew
Midnight Rain              Ralls, Kim
Maeve Ascendant            Corets, Eva

also... as noted at the link below... if you don't move or rename the temp file, it will be deleted when the php script ends. php:: how long to tmp files stay?

Community
  • 1
  • 1
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59