PHP 5.4 multipart/form-data UTF-8 encoding
Asked Answered
M

11

6

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?

Motteo answered 11/6, 2013 at 1:59 Comment(0)
M
3

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.

Motteo answered 8/3, 2014 at 23:30 Comment(0)
W
5

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)
Whitefaced answered 27/6, 2013 at 16:51 Comment(1)
Thank you for the answer but the data will be inserted in MySQL table with UTF-8 charset, so converting it to ISO-8859-9 would be another trouble.Motteo
M
3

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.

Motteo answered 8/3, 2014 at 23:30 Comment(0)
L
1

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.

Lewison answered 7/12, 2016 at 18:41 Comment(0)
C
0

you should to try to re-install your wamp or xampp or your apache and php.and run your code on some one else's machine with the same php version .if this code runs then try to figure out why it is not working in your server or check of file_upload extension in your php.

Contour answered 30/6, 2013 at 19:28 Comment(0)
S
0

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

Societal answered 1/7, 2013 at 8:31 Comment(0)
N
0

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...

Natascha answered 1/7, 2013 at 17:3 Comment(0)
C
0

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.

Cerebrospinal answered 2/7, 2013 at 23:49 Comment(0)
R
0

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.

Roseline answered 3/7, 2013 at 13:46 Comment(0)
P
0

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.

Peltate answered 3/7, 2013 at 15:7 Comment(0)
A
0

mb_internal_encoding("UTF-8");

Add this code before your string..

Alar answered 24/2, 2014 at 14:21 Comment(0)
Q
0

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

Queen answered 19/6, 2015 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.