14

I'm uploading a file with XMLHttprequest. Here is the JS function, that uploads a file:

var upload = function(file) {
    // Create form data
    var formData = new FormData();
    formData.append('file', file);

    var xhr = new XMLHttpRequest();

    // Open
    xhr.open('POST', this.options.action);

    // Set headers
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    xhr.setRequestHeader("Content-Type", "multipart/form-data");
    xhr.setRequestHeader("X-File-Name", file.fileName);
    xhr.setRequestHeader("X-File-Size", file.fileSize);
    xhr.setRequestHeader("X-File-Type", file.type);

    // Send
    xhr.send(formData);
}

On the server side, in upload.php I read the file this way:

file_put_contents($filename, (file_get_contents('php://input')));

Everything works fine, except that I get a PHP Warning:

Missing boundary in multipart/form-data POST data in Unknown on line 0.

If I remove this line: xhr.setRequestHeader("Content-Type", "multipart/form-data"); the warning goes away.

What should be the problem here?

6
  • 2
    Try using application/x-www-form-urlencoded as content type. Or leave it out, since this is the default content-type sent by an ajax request. Sep 10, 2012 at 9:43
  • 1
    This can work, but for large files (even 5-600 KB) the browser will freeze because it has to send the file in a "text" format. I have to use the multipart/form data.
    – Tamás Pap
    Sep 10, 2012 at 9:55
  • 3
    Sorry, overlooked the file-upload bit (don't know how), but I think this might answer your queston, more specifically: xhr.setRequestHeader("content-type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2)); should do the trick Sep 10, 2012 at 10:00
  • Also tried this solution. The warning dissapears, but in this case no data is coming: file_get_contents('php://input') is empty. Appreciate your help Elias, and if you have any other idea, please tell me.
    – Tamás Pap
    Sep 10, 2012 at 10:10
  • I believe the issue is that you're missing boundary in your "Content-Type" header. It should look something like: Content-Type: multipart/form-data; boundary="same-as-the-boundary-in-request-body" This is usually created for you if you omit setting the "Content-Type" header May 10, 2013 at 12:10

1 Answer 1

20

Well this is strange a little bit for me, but this is what worked:

// Open
xhr.open('POST', this.options.action, true);

// !!! REMOVED ALL HEADERS

// Send
xhr.send(formData);

In this case, on server side I don't read the file sent via php://input but the file will be in the $_FILES array.

This solved my problem, but I'm still curious why appears now the file in $_FILES?

Tested in Chrome, Mozilla, Safari, and IE10.

2
  • 13
    The spec explains (point 3) that the browser sets the correct headers (including the correct multipart boundary indication in the Content-Type) if you haven't manually specified anything. The file appears in $_FILES because PHP automatically puts files from multipart form uploads in that superglobal.
    – Gijs
    Sep 10, 2012 at 10:54
  • 1
    I had to comment out this for it to work xhr.setRequestHeader('Content-Type', 'multipart/form-data');...... (but kept xhr.setRequestHeader('Accept', 'multipart/form-data'))
    – MCH
    Jul 23, 2020 at 10:55

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.