Jump to content

Upload xml files in a folder to Mysql using Php using glob and array


BruceFarmerson
 Share

Recommended Posts

I have several xml files in a folder on my webserver. I want to

1. look in the folder
2. get xml file names
3. put file names into array
4. loop those names into a mysql query
5. have contents of all xml files inserted into db

 

     $files = glob('*.xml');
    
    foreach($files as $file){
        mysql_query("LOAD DATA INFILE '".$file."' INTO TABLE tablename ROWS IDENTIFIED BY ''");
    
    }

 




I've spent a couple of days looking for an example or tutorial but can't find anything online.

Above is the best I can come up with.

Any help appreciated :)

Link to comment
Share on other sites

The only time I've ever used this is to load a CSV file into MySQL. I've checked the documentation and I can't see that XML is supported using this function. It looks relatively simple looking for a field delim and new lines. New lines identifying the end of the row. It's worth pointing out that XML does not need to follow this convention to be valid:

 

<parent><child>Im a babeh!</child></parent><parent><child>Im a babeh!</child></parent>

 

The above is valid XML but two records are on the same line. So LOAD DATA INFILE function wouldn't know where one row stops and the other begins.

 

You might be able to fudge around it with adding custom delims but you may encounter problems. The safest solution would be to create a CSV using PHP, you'll have more control over handeling formatting issues and it'd only take a few lines using SimpleXMLIterator and SPLFileObject.

 

If you need any more help then give me a shout

Edited by exeTrix
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.