Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.4 multipart/form-data UTF-8 encoding

I'm having problem with UTF-8 encoding while posting form data as "multipart/form-data", without multipart/form-data everything works well. But since I have to upload files on same post, I need to use multipart/form-data.

Problem started after upgrading from PHP 5.3.x to PHP 5.4.4-14 (bundled with Debian Wheezy), same scripts works well with PHP 5.3 test server.

  • All of my documents are saved in UTF-8 and has <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> tags.
  • I tested with different browsers on different computers
  • mb_detect_encoding() detects posted string as UTF-8
  • I tried AddDefaultCharset utf-8 for Apache configuration.

Here you can test my scripts, you can copy/paste following string with Turkish characters (ex. string: öçşipğopüp )

http://sa.chelona.com.tr/haber-ekle.html

I also found related question at UTF-8 text is garbled when form is posted as multipart/form-data in PHP but it recommends re-installing apache/php and that's not possible for my situation. Is this a known PHP/Apache bug?

like image 386
she hates me Avatar asked Jun 11 '13 01:06

she hates me


3 Answers

Do a simple conversion from UTF-8 to Turkish Alphabet ISO-8859-9 and the problem should be solved

iconv('UTF-8', "ISO-8859-9", $string);

Example Input : öçşipğopüp

Example Form:

<form method="post" enctype="multipart/form-data" action ="self.php">
<input type="text" name="hello" />
<input type="submit" name="test" />
</form>

Simple Sump :

var_dump($_POST['hello'],iconv('UTF-8', "ISO-8859-9", $_POST['hello']));

Output

string 'öçşipğopüp ' (length=16)
string 'öçþipðopüp ' (length=11)
like image 164
Baba Avatar answered Sep 28 '22 07:09

Baba


I'm writing this to answer my own question... I hope it will help somebody else...

if you use PHP 5.4.x, setting mbstring.http_input from "auto" to "pass" may solve your problem.

like image 39
she hates me Avatar answered Sep 28 '22 07:09

she hates me


My php version is 5.4.45 and changing mbstring.http_input from auto to pass works very well. In php.ini file the default value is pass. For more detail about this variable you can see here.

like image 21
rukishi Avatar answered Sep 28 '22 06:09

rukishi