Read an uploaded XML file

Hello,
I would like to ask about how I can pass the file using $_FILES as a parameter for simplexml_load_string
I’m trying the following:
$xml=simplexml_load_string($_FILES[‘file’][‘name’]);
print_r($xml);
but I’m getting the following error message:
simplexml_load_string(): Entity: line 1: parser error : Start tag expected, ‘<’ not found

$_FILES is a system array, not really a file. Therefore, you have to load the file in the normal manner, save it to a folder so that it is no longer a temporary file, then load it from the stored location.
The server uploads the file and places it in a temp file and creates a name for it. But, remember any error can cause it to go away.

Now with all that said, you can load it by using the temporary file name that the server creates for the file that is uploaded. Something like this should work:
$xml=simplexml_load_file($_FILES[‘file’][‘tmp_name’]);
Remember handling it this way does not give you any any audit trail and no way to go back if there is an error. The temp file will just disappear once this process is completed.

By the way, welcome to the site!

1 Like

Thank you very much for your help. I should thank you for your great explain since I understand the problem now
Unfortunately, I used your solution but I’m ending with the same error
I’m sorry because this can seems a little stupid since I’m totally new to PHP, I moved the uploaded file into a specific path, like this:
$oldPath = $_FILES[‘file’][‘tmp_name’];
$newPath = ‘/tmp/’ . basename($_FILES[‘file’][‘name’]);
move_uploaded_file($oldPath, $newPath)

how I can use this path to access the file for XML Prase
Finally, glad to be apart from the community

Well, perhaps I should ask you what you do with this XML data? I don’t care about it’s use, but, what I mean is, are these just temporary files that do not need to be kept or tracked? Is this just for you or are a lot of your users going to upload a lot of XML files? Just curious as it might make a difference on how it should work code-wise.

Normally this error means that the input is NOT an XML file. XML syntax starts off with a heading of sorts. Are you sure the file is XML? Here is an example of one…

<?**xml** version="1.0" encoding="ISO-8859-1"?>

< email>
< to>Ernie< /to>
< from>Not-Ernie< /from>
< /email>
**** Not at all well-formed, but, just a sampler ****
If the input file is a doc, you would need to use simpledoc not simplexml. Can you give us an idea what is in the file you are testing with?

What I need to do is the following:
1- let the user upload XML file (only dealing with one user)
2- store this XML file into the server
3- view it to the user (read it)
4- transfer it to a text file
I did 1 & 2, but I have this problem in 3 & 4, viewing this uploaded XML file
user will upload one file only, and it is okay if the file is temporary because I just need to view it and make it a text file
Finally, I’m sure that I have an XML file (because I tried different ones)
Thank you in advance

Well, from that description, the logic seems odd. If you just want it to be a text file, just convert it.
XML files are actually just text files. They are text with a certain format. Therefore, just copy the file as a text file. And, you do not need to use simplexml just to display a text file. Confused on why you would need all those extra steps. Unless you need to pull out certain XML sections. Lets look at each step.

1 - you tested as the user and selected a file.
2 - you posted the file and uploaded it to the server in a folder or wherever
3 - you know the file is stored in a certain place and have the new file-name just view it like this:
$file_data=file_get_contents($filename);
echo $file_data;
(This would display a text file or XML file since they are both just text, but, you might need formatting)
4 - no need for this at all if you just store it as a .txt file in #2

Do you understand my thoughts?

1 Like

Yes I understand what you mean
you are right, I’m going to adopt this
Thank you very much for all your help

If you need to really use XML, I can create an example of it’s use tomorrow for you. Late here now and heading to bed. Good luck with it.

1 Like

Thank you very much!

Sponsor our Newsletter | Privacy Policy | Terms of Service