Read XML received from ajax - PHP

3

In php using the command var_dump($_FILES ['filexml']); I get the following ajax values

array(5) {
  ["name"]=>
  string(56) "nomeficticio.xml"
  ["type"]=>
  string(8) "text/xml"
  ["tmp_name"]=>
  string(14) "/tmp/phpoqnomeficticio"
  ["error"]=>
  int(0)
  ["size"]=>
  int(16536)
}

Through this data, how can I read this XML file? with PHP I even tried to use the simplexml_load_file($_FILES['filexml']); command but gave the following error:

  

simplexml_load_file () expects parameter 1 to be a valid path, array   given

    
asked by anonymous 10.09.2015 / 15:07

2 answers

3

The array already tells you where the file is located. Just open it:

simplexml_load_file($_FILES['filexml']['tmp_name']);

However, it is not recommended to treat a file that is in a temporary directory, since the OS itself can clean this directory from time to time. Prefer to move it before that:

$basename = basename($_FILES['filexml']['tmp_name']);
move_uploaded_file($_FILES['filexml']['tmp_name'], __DIR__ . $basename);
simplexml_load_file( __DIR__ . $basename);
    
10.09.2015 / 15:23
3

You just need to use 'tmp_name' instead of 'name'. Here's an example:

<?php
if(isset($_FILES['input_file'])){
    $xml = simplexml_load_file($_FILES['input_file']["tmp_name"]);
    echo '<pre>';
    var_dump($xml);
    echo '</pre>';
}
?>
<form class="frm" method="post" enctype="multipart/form-data" novalidate="novalidate">
    <input type="file" name="input_file"/>
    <input type="submit" value="Enviar"/>
</form>

I hope I have helped!

If it works, I'd be happy to get upvote and choose my answer.

    
10.09.2015 / 15:23