Reputation: 1272
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.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
tags.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?
Upvotes: 6
Views: 6646
Reputation: 11
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.
Upvotes: 1
Reputation: 21
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
Upvotes: 0
Reputation: 1272
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.
Upvotes: 3
Reputation: 24131
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.
Upvotes: 0
Reputation: 4072
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.
Upvotes: 0
Reputation: 802
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.
Upvotes: 0
Reputation: 1286
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...
Upvotes: 0
Reputation: 1773
if uncommenting the default charset line in php.ini does something, ot will be easy to fix. remember to bounce apache after changing.
Upvotes: 0
Reputation: 1279
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.
Upvotes: 0
Reputation: 95131
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)
Upvotes: 5