0

i am trying to send a file through xmlhttprequest and process it on the server through PHP, i cannot use jQuery.

Here is my javascript

xhr = new XMLHttpRequest();
xhr.open("POST", $("upload").action, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader("X_FILENAME", file.newname || file.name);
xhr.setRequestHeader("CONTENT_TYPE", file.type);
xhr.send("userfile=" + file + "&" + $('upload').serialize());

it happens that on my PHP the $_FILES is empty and the $_POST along with the form data holds the variable userfile which is marked as "object file".

So now my questions are:

  1. Does the variable userfile really holds the content of the file i am trying to upload?
  2. How can i copy the value in userfile to a file on my server?

This is what I tried so far:

file_put_contents($tmpfile, $userfile);

but it does not seem to copy correctly the value into $tmpfile.

I hope I was clear enough

Thanks in advance

Best regards

Donnie
  • 113
  • 8
  • "Does the variable userfile really holds the content of the file i am trying to upload? —Read the [documentation for serialize](http://api.jquery.com/serialize/), it says: "Data from file select elements is not serialized" – Quentin Mar 20 '15 at 11:48
  • Infact I am sending the file in another variable, named "userfile" not as part of the serialized parameters. – Donnie Mar 20 '15 at 11:51
  • `file` isn't defined in that code, and (even if it was) it certainly wouldn't be encoded in a way that would appear in PHP's `$_FILES`. The correct way to deal with uploading files is in the duplicate question. – Quentin Mar 20 '15 at 16:01

0 Answers0