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?
application/x-www-form-urlencoded
as content type. Or leave it out, since this is the default content-type sent by an ajax request.multipart/form data
.xhr.setRequestHeader("content-type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2));
should do the trick