1

I have a file that I use to upload XML files to my webserver using PHP but I have problem with some XML files and I would like to change its coding from utf-8 to ISO-8859-1 before upload that file, is this possible with a native php function or I need to do that from scratch?

I mean to read the file before upload change that part with str_replace maybe and then save the xml file?

This is part of my code

if (move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])) {

    $xml = simplexml_load_file($upload_dir.$pic['name']);

It should be before the if statement, what do you think?

Fixer
  • 175
  • 1
  • 2
  • 16
  • This post should help you to convert the xml string to ISO-8859-1 http://stackoverflow.com/questions/374425/convert-utf8-characters-to-iso-88591-and-back-in-php. You can (i would) do this before saving the file. Simply use simplexml_load_string($pic['tmp_name']) and save the converted result to a file using file_put_contents($upload_dir.$pic['name'], $yourConvertedXmlString). – Yolo Mar 09 '17 at 19:14

2 Answers2

1

You can not change files before upload, only after you got your copy.

An XML file includes its encoding in the XML declaration. With this information the XML parser decodes it. SimpleXML and the other PHP XML Apis use all UTF-8. Any property or method will return UTF-8 strings.

The only exception are the serialization methods like asXml(). The will use the encoding as defined for the document.

So here is no real point in converting it to ISO-8859-1, loading it into SimpleXML will just convert it back. The two possible effects of the conversion is that you will loose characters not available in ISO-8859-1 or that you break the XML.

ThW
  • 19,120
  • 3
  • 22
  • 44
  • Thanks for your answer but I solved my problem, I will put it as answer for anyone else having the same problem – Fixer Mar 10 '17 at 23:31
0

I solved my problem doing this for anyone else with the same issue, my problem was that I have an app where I can upload XML files and the encoding of those files is utf-8 but some files for some reason are bad formatted because the encoding is not utf-8 but ISO-8859-1 so when I was trying to upload this corrupts XML the the javascript function returned a warning like "parsererror" "SyntaxError: Unexpected token < in JSON at position 0 blabla so I did this

if (move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])) {


    $xml = @simplexml_load_file($upload_dir.$pic['name']);


    if ($xml === false) {


        $data = file_get_contents($upload_dir.$pic['name']);
        $data = utf8_encode($data);

        file_put_contents($upload_dir.$pic['name'], $data);


        $xml = simplexml_load_file($upload_dir.$pic['name']);
    }

I suppresed the warning of simplexml_load_file function, maybe is not the best way to do this but for me this was my solution, after that I get the content of the xml file and convert it to utf-8 and then put the content back to the file and now I can get the data correctly to do what I need

Fixer
  • 175
  • 1
  • 2
  • 16