Take the 2-minute tour ×

I'm trying to upload file, using XMLHTTPRequest, and sending this headers:

Content-Type:multipart/form-data, boundary=xxxxxxxxx

Content-Disposition: form-data; name='uploadfile'; filename='123_logo.jpg'
Content-Transfer-Encoding: base64
Content-Type: image/jpeg

But on server side PHP ignore header "Content-Transfer-Encoding: base64" and write base64 undecoded data directly into the file!

Is there any way to fix it?

p.s. it is very important to send data using base64

share|improve this question
Why do you construct the headers manually? Why is it significant to send it base64-encoded rather than binary? –  mario Mar 2 '11 at 15:18
Because there is no implemented method xhr.sendAsBinary() in Google Chrome. –  Scalar Mar 2 '11 at 15:26
add comment

2 Answers

up vote 1 down vote accepted

In fact there is no Content-transfer-encoding in HTTP see


19.4.5 No Content-Transfer-Encoding


but you can use base64_decode as in http://php.net/manual/en/function.base64-decode.php

share|improve this answer
Thnx! Using base64_decode is very ugly method, but i think i have no other choice. –  Scalar Mar 2 '11 at 15:26
The older RFC1867 for multipart/form-data is probably also relevant. It mentions Content-Transfer-Encoding for mail transports, but there's no need in HTTP. This is why PHPs rfc1867.c does not honor it. –  mario Mar 2 '11 at 15:35
add comment

Xavier's answer doesn't sound right. RFC2616 also has this to say (section 3.7):


It seems to me that section 19.4 of RFC2616 is talking about HTTP as a whole, in the sense that it uses a syntax similar to MIME (like headers format), but is not MIME-compliant.

Also, there is RFC2388. In section 3, last paragraph, it says:


Section 4.3 elaborates on this:

4.3 Encoding


share|improve this answer
Agreed. This is definitely a bug in PHP. Both the W3C HTML docs and the RFCs you highlight make quite clear that in a POST response to a form, Content-Transfer-Encoding may be used. This makes sense because otherwise text fields might inadvertently include a MIME boundary sequence, and would all have to be sent in binary with a Content-Length. –  alastair Mar 27 '12 at 14:05
This isn't a "bug" in PHP, nor is it particular to PHP. From my research into this, it appears multipart POST is a complete mess, IETF RFC are mostly useless and needs to be updated. RFC 2388 needs to be completely overhauled, so does HTML 4.01's w3.org/TR/html401/interact/forms.html reference. Basically. No modern UA use content-transfer-encoding, you must use content-type to determine the encoding. Not only that, charsets is assumed to be the same as the initial requested form's charset, and "multipart/mixed" for multiple files is not used. Just as well; KISS. –  Ronald Chan May 8 '13 at 10:37
add comment

Your Answer


