PHP 5.4 multipart / form-data UTF-8 encoding

I had a problem with UTF-8 encoding when sending form data as "multipart / form-data", without multipart / form data, everything works fine. But since I have to upload files to the same post, I need to use multipart / form-data.

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

  • All my documents are saved in UTF-8 and have tags <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> .
  • I tested various browsers on different computers.
  • mb_detect_encoding () detects the published string as UTF-8
  • I tried AddDefaultCharset utf-8 to configure Apache.

Here you can test my scripts, you can copy / paste the following line with Turkish characters (for example, line: öçşipğopüp)

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

I also found the related question in the UTF-8 text is distorted when the form is placed as multipart / form-data in PHP , but recommends reinstalling apache / php and this is not possible for my situation. Is this a known PHP / Apache bug?

+6
source share
11 answers

I am writing this to answer my question ... I hope this helps someone else ...

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

+3
source

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

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

Input Example: öçşipğopüp

Form Example:

 <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) 
+5
source

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

+1
source

you should try reinstalling your wamp or xampp or your apache and php. and run your code on some other machine with the same php version. If this code runs, try to find out why it does not work on your server or check the file_upload extension on your php.

0
source

if uncommenting the default charset line in php.ini does something, ot will be easy to fix. do not forget to refuse apache after change.

0
source

I do not think you should use mb_detect_encoding to determine the encoding in this case.

If you should use it, perhaps you need to set the detection order to make sure that UTF-8 is above the list, see http://www.php.net/manual/en/function.mb-detect-order.php

You have set the accept-charset form to UTF-8; you set the source page to UTF-8: all current browsers will send UTF-8. HTML 5 points to 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, do not try to convert it, and everything will be fine.

But if you host some of your PHP code, then it may be clear what you are trying to do and what is wrong ...

0
source

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

0
source

You need to add headers in PHP and HTML, for example, in lower case:

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

0
source

Your sample page looks correct, and the steps you take seem to cover most of the important points, there is one more thing that I would check. You wrote that the data is stored in the MySql database with UTF-8 encoding, but this does not necessarily mean that the PHP connection object also works with this encoding.

 // 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 above examples show how to set the encoding for SQLI or PDO. Thus, the preparation of the connection object makes you independent of the database configuration, if necessary, the connection even converts the returned / sent data.

To check this on your page, make sure that the character set is installed before inserting / querying the database.

0
source

mb_internal_encoding ("UTF-8");

Add this code before the line.

0
source

After a long time, trying to unzip () and the sentences from the answers here, I found an error, and maybe you have the same reason for the encoding problem.

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

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

This is for php 5.2.xx

0
source

Source: https://habr.com/ru/post/947002/


All Articles