简体   繁体   中英

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?

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)

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.

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 .

你应该尝试重新安装你的wamp或xampp或你的apache和php.and在其他人的机器上使用相同的php版本运行你的代码。如果这个代码运行然后试图弄清楚它为什么它在你的服务器中不起作用或检查你的php中的file_upload扩展名。

if uncommenting the default charset line in php.ini does something, ot will be easy to fix. remember to bounce apache after changing.

I don't think you should be using mb_detect_encoding to determine the encoding in this case.

If you must use it, then maybe you need to set the detection order to make sure UTF-8 is higher up the list, see http://www.php.net/manual/en/function.mb-detect-order.php

You've set the form's accept-charset to UTF-8; you've set the original page to UTF-8: all current browsers will send UTF-8. HTML 5 specifies this FWIW: http://www.w3.org/TR/2011/WD-html5-20110405/association-of-controls-and-forms.html#multipart-form-data

The string will be UTF-8, don't attempt any conversion of it, and you will be fine.

But if you post some of your PHP code then maybe it will be clear what you're trying to do and what's going wrong...

Sorry this is more of an idea for a workaround than actual solution, however if all traditional methods have failed, and you can't reinstall anything, try converting from the UTF8 code points. Something like using a base64 encoding before sending and then decode on receive. Or convert to a hex string and decode after receiving.

You need add headers in PHP and HTML, like lowercase:

    <?php header('content-type: text/html; charset=utf-8'); ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <form method="post" enctype="multipart/form-data" action ="self.php">
        ...
    </form>
    </body>
    </html>

Remember: Save all php and html files in utf-8 Without BOM.

Your example page looks correct and the steps you have taken seem to cover most of the important points, there is one more thing i would check though. You wrote that the data is stored in a MySql database with UTF-8 charset, but this doesn't necessarily mean, that the PHP connection object works with this charset too.

// tells the mysqli connection to deliver UTF-8 encoded strings.
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);
$db->set_charset('utf8');

// tells the pdo connection to deliver UTF-8 encoded strings.
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$db = new PDO($dsn, $dbUser, $dbPassword);

The examples above show how to set the charset for SQLI or PDO. Preparing the connection object this way, makes you independent of the database configuration, if necessary the connection will even convert the returned/sent data.

To test this in your page, make sure that the charset is set, before inserting/querying the database.

mb_internal_encoding("UTF-8");

Add this code before your string..

After a long time trying with unpack() and the proposals from the answers here, I found a pitfall, and maybe you have the same reason for the encoding problem.

All I had to do was making htmlentities using utf-8 explicitly:

htmlentities(stripslashes(trim(rtrim($_POST['title']))), ENT_COMPAT, "utf-8");

This is for php 5.2.xx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM