PHP

RFC 2047 multipart form decoding in PHP

  • PHP
  • RFC2047

Sometime, there are error on the php backend when uploading the file. When I check the form data and it was encoded with RFC 2047.

=?utf-8?B?L2RhdGEvdXNlci8wL2NvbS5mdXp6eXBhd3Muc25zL2ZpbGVzL+iMuOS5i+eIqsK356S+5Yy6L0ltYWdlcy8yMDIwMDUwODIyNTExMjMzODMuanBlZw==?=

This is because the file path contains non-US ACSII character, so the request programe will try to encode it first. There is no way to extract file name from the raw data. The easy way to solve it is using iconv_mime_decode function to decode the file name first.

echo iconv_mime_decode('=?utf-8?B?L2RhdGEvdXNlci8wL2NvbS5mdXp6eXBhd3Muc25zL2ZpbGVzL+iMuOS5i+eIqsK356S+5Yy6L0ltYWdlcy8yMDIwMDUwODIyNTExMjMzODMuanBlZw==?=', 0);

 The filename will output correctly.

Comments