0

I am trying upload xml file using simplexml_load_file but when in file is special character i'm getting an error ( There is everything alright without special character in it). First of all, I am using XML file from another program so i can't change it.

This is sample of xml:

<wuo>
   <header>
      <title>title1</title>
   </header>
   <body>
      <lp>1</lp>
      <sign>124.455</sign>
      <text>sample text with & character</text>              //<-- this is causing the problem
   </body>
   <body>
      <lp>2</lp>
      <sign>12556.455</sign>
      <text>sample text 2</text>
   </body>
</wuo>

My code:

if(isset($_POST["submit"])) 
{
    if(isset($_FILES['wuo']) && ($_FILES['wuo']['error'] == UPLOAD_ERR_OK)) 
    {
        if(!simplexml_load_file($_FILES['wuo']['tmp_name'])) 
        {
            // if file has special character in it this fires up
            echo 'Error';
        }
        else
            $file = simplexml_load_file($_FILES['wuo']['tmp_name']);
       
        print_r($file);
        /**
           showing file to html etc... unimportant code for this case
        **/
    }
    else
        echo 'Error:' . $_FILES['wuo']['error'];
}

I know, I should do something before simplexml_load_file but i don't know what exaclty. Or maybe I should use something else...

Btw: I don't need to secure it because this is only for me.

Kafus
  • 31
  • 5

1 Answers1

0

Your XML is not valid. An ampersand must be escaped as &amp;.

If you can't change it yourself, you will need to pre-process it in some way before passing it to an XML parser.

wasmachien
  • 969
  • 1
  • 11
  • 28
  • how :) any suggestions? – Kafus Sep 14 '20 at 22:37
  • In general, you can't repair ill-formed XML unless you know what kind of illformities you are looking for. The best advice is, don't accept ill-formed XML in the first place. Correct the process that generates it. If it comes from a third party, reject it as you would any other faulty goods. – Michael Kay Sep 14 '20 at 22:57