You are here:  » How to upload XML file & analyze before insertion to MySQL table


How to upload XML file & analyze before insertion to MySQL table

Submitted by kevinkos on Mon, 2006-09-25 11:06 in

Hi. I just bought Magic Parser. I am looking for a solution where people can upload xml files through a web interface to a MySql table. But now I am considered about the way to first browse for a file (like your demo Option 1). I thought it should work with the " $filename = $_GET['tosfile'];" in the target page for the form, but I only get an errormessage: "could not open". If I upload the file manually through ftp and changes the $filename = xxx.xml (the same file that I am trying to analyze through the form upload) it works.

Further I wonder if somebody has managed to do something similiar to what I wish to do. I have a site running wher I have a kind of on-stock list from different suppliers in one MySQL table. Now I want to give the suppliers an opportunity to upload their complete stock lists (not only new items but the whole list) through a standardised XML file from their own computer. The user interface should handle:

1. Browse for a XML file to import
2. Analyze the content (preview)
3. Delete old entries from MySQL table (only this supplier´s entries)
4. Update MySQL table

I am not so good at PHP, therefore I as for some guidance in this case...
Thanks!

Submitted by support on Mon, 2006-09-25 12:27

Hi Kevin,

First things first, if you want to upload files through the web browser you need to use PHP's multi-part form encoding method, which is documented here:

http://uk.php.net/manual/en/features.file-upload.php

Here's a basic script to get you started. This will accept a file upload (assuming that it is enabled on your PHP installation) and then parse it using Magic Parsers's autodetection (just like the demo option 1). It only displays the content of the first record:

upload.php

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
"<table border='1'>";
    foreach(
$record as $k => $v)
    {
      print 
"<tr>";
      print 
"<th>".$k."</th>";
      print 
"<td>".$v."</td>";
      print 
"</tr>";
    }
    print 
"</table><br /><br />";
    return 
true;
  }
  if (
$_POST["action"] == "upload")
  {
    if (
$_FILES["file"]["tmp_name"])
    {
      
$filename $_FILES["file"]["tmp_name"];
      
MagicParser_parse($filename,"myRecordHandler");
    }
    else
    {
      print 
"<p>Upload failed.</p>";
    }
  }
  print 
"<form enctype='multipart/form-data' method='POST'>";
  print 
"<input type='hidden' name='action' value='upload' /> ";
  print 
"<input type='file' name='file' /> ";
  print 
"<input type='submit' value='Submit' /> ";
  print 
"</form>";
?>

Now, notice how this script parses the temporary file that PHP has created during the upload. This file is automatically deleted as soon as the script finishes. To keep the file you have to move it somewhere else with the move_uploaded_file() function.

Hope this helps!
Cheers,
David.