Table of Contents
Solution to solve the problem of garbled Chinese file names uploaded by PHP,
Home Backend Development PHP Tutorial Solution to the problem of garbled Chinese file names uploaded by PHP, _PHP tutorial

Solution to the problem of garbled Chinese file names uploaded by PHP, _PHP tutorial

Jul 13, 2016 am 10:07 AM
Chinese garbled

Solution to solve the problem of garbled Chinese file names uploaded by PHP,

Uploading files in PHP is the most basic technical point, but there are many problems that need to be solved when going deeper. No, after uploading a Chinese file, the file name becomes garbled.

The following is the problem code, it is very simple:

1. Problem code

html part:

Copy code The code is as follows:



enctype="multipart/form-data">








php part:

Copy code The code is as follows:

if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
If (file_exists("upload/" . $_FILES["file"]["name"]))
{
              echo $_FILES["file"]["name"] . " already exists. ";
}
      else
{
       move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
}
}

Uploaded a file named "test data.txt", oh ho, the file was uploaded, but the file name was garbled.

2. First test

Search for solutions online and find

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);

changed to

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . iconv("UTF-8","gbk",$_FILES["file"]["name"]));

It turns out that the return value of iconv function is false.

Check the function manual and find that the second parameter has special usage. A simple translation is that I can append //TRANSLIT or //IGNORE after the encoding. The former will convert the untranslatable characters into the closest characters. , the latter simply ignores characters that cannot be converted.

Try it:

Copy code The code is as follows:

var_dump( iconv("UTF-8","gbk//TRANSLIT",$_FILES["file"]["name"]));
var_dump( iconv("UTF-8","gbk//IGNORE",$_FILES["file"]["name"]));

Result:

bool(false) string(4) ".txt"

That is to say, Chinese characters cannot be converted, and there are not even close characters. It seems that the methods introduced online are not omnipotent.

3. The online introduction method fails, try again

Let me guess, maybe my system will cause garbled characters when creating Chinese files, so I rewrote the code:

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/test data.txt");

The result was successfully created without garbled characters. . . In other words, it is not a system problem.

Think about it, my php file itself is utf8 encoded, then

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/test data.txt");

This statement must use utf8 encoding, so the file name uploaded before must not be utf8 encoded, then the following statement must be wrong, because the source string itself is not utf8 encoded:

Copy code The code is as follows:

iconv("UTF-8","gbk//TRANSLIT",$_FILES["file"]["name"]);

Use the function to check the encoding of the source string:

Copy code The code is as follows:

$e=mb_detect_encoding($text, array('UTF-8', 'GBK','gb2312'));
echo $e;

The result is CP936, that is, the source string encoding is GBK.

Try it

Copy code The code is as follows:

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . iconv("gbk","UTF-8",$_FILES["file"]["name"]));

Problem solved, no more garbled characters

4. Another solution

In fact, there is another solution, which is to add

in the middle of the head tag of the html file.

Copy code The code is as follows:


This keeps the encoding consistent and eliminates the need to transcode

5. The following is the conclusion

Using the iconv function can solve the problem of garbled Chinese file names when uploading. In fact, iconv can solve various garbled problems caused by inconsistent encoding.
Please check the encoding of the source string first when using the iconv function, unless you have already determined the encoding of the source string.
Try to ensure that all codes are encoded consistently, and only use the iconv function as a last resort.
To make a complaint, try not to use Chinese file names as file names saved on the server. Please convert the file names into your own file names (please convert even English file names).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/953152.htmlTechArticleSolution to solve the problem of garbled Chinese file names uploaded by php. Uploading files in php is the most basic technical point, but in-depth There are a lot of problems that need to be solved after entering. No, after uploading the Chinese file...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Methods to solve the problem of Chinese garbled characters in PHP Dompdf Methods to solve the problem of Chinese garbled characters in PHP Dompdf Mar 05, 2024 pm 03:45 PM

Methods to solve the Chinese garbled problem of PHPDompdf PHPDompdf is a tool for converting HTML documents to PDF files. It is powerful and easy to use. However, when processing Chinese content, you sometimes encounter the problem of garbled Chinese characters. This article will introduce some methods to solve the Chinese garbled problem of PHPDompdf and provide specific code examples. 1. When using font files to process Chinese content, a common problem is that Dompdf does not support Chinese content by default.

The ultimate solution to the problem of Chinese garbled characters in PyCharm The ultimate solution to the problem of Chinese garbled characters in PyCharm Jan 27, 2024 am 08:00 AM

The ultimate method to solve the problem of Chinese garbled characters in PyCharm requires specific code examples. Introduction: PyCharm, as a commonly used Python integrated development environment (IDE), has powerful functions and a friendly user interface, and is loved and used by the majority of developers. However, when PyCharm processes Chinese characters, it may sometimes encounter garbled characters, which causes certain problems in development and debugging. This article will introduce how to solve the Chinese garbled problem in PyCharm and give specific code examples. 1. Set up the project

Common causes and solutions for Chinese garbled characters in MySQL installation Common causes and solutions for Chinese garbled characters in MySQL installation Mar 02, 2024 am 09:00 AM

Common reasons and solutions for Chinese garbled characters in MySQL installation MySQL is a commonly used relational database management system, but you may encounter the problem of Chinese garbled characters during use, which brings trouble to developers and system administrators. The problem of Chinese garbled characters is mainly caused by incorrect character set settings, inconsistent character sets between the database server and the client, etc. This article will introduce in detail the common causes and solutions of Chinese garbled characters in MySQL installation to help everyone better solve this problem. 1. Common reasons: character set setting

What to do if ajax transmits Chinese garbled characters What to do if ajax transmits Chinese garbled characters Nov 15, 2023 am 10:42 AM

Solutions for ajax to transmit Chinese garbled characters: 1. Set a unified encoding method; 2. Server-side encoding; 3. Client-side decoding; 4. Set HTTP response headers; 5. Use JSON format. Detailed introduction: 1. Set a unified encoding method to ensure that the server and client use the same encoding method. Under normal circumstances, UTF-8 is a commonly used encoding method because it can support multiple languages ​​​​and character sets; 2 , Server-side encoding. On the server side, ensure that the Chinese data is encoded in the correct encoding method and then passed to the client, etc.

Methods to solve the problem of Chinese garbled characters in Linux system Methods to solve the problem of Chinese garbled characters in Linux system Feb 19, 2024 am 09:22 AM

The problem of Linux Chinese garbled characters is a problem that many Chinese users often encounter when using Linux systems. The main reason for garbled Chinese characters is that the default character encoding used by the Linux system is UTF-8, but some software or applications may not be compatible with UTF-8 encoding, causing Chinese characters to not be displayed correctly. There are many ways to solve this problem. Several common solutions will be detailed below and specific code examples will be provided. Modify the terminal character encoding settings: The terminal character encoding settings determine whether the terminal can correctly

What should I do if the PHP web page has Chinese garbled characters? A complete solution What should I do if the PHP web page has Chinese garbled characters? A complete solution Mar 26, 2024 pm 03:27 PM

The problem of Chinese garbled characters in PHP web pages is that Chinese characters are displayed as garbled characters in the web page display. This situation is usually caused by inconsistent encoding or the character set is not set. Solving the problem of Chinese garbled characters in PHP web pages requires starting from many aspects. The following are some common solutions and specific code examples. Set the PHP file encoding: First make sure that the encoding of the PHP file itself is UTF-8. You can set the UTF-8 encoding when saving in the editor, or add the following code to the header of the PHP file to set the encoding: &l

A practical method to effectively solve the problem of Chinese garbled characters in Eclipse A practical method to effectively solve the problem of Chinese garbled characters in Eclipse Jan 03, 2024 pm 05:50 PM

Practical tips for quickly solving Chinese garbled characters in Eclipse require specific code examples. Overview: Eclipse is a widely used integrated development environment (IDE) that not only supports the development of multiple programming languages, but also supports multiple operating systems. However, sometimes when using Eclipse, we may encounter the problem of Chinese garbled characters, which brings inconvenience to our development work. This article will introduce some practical techniques to help us quickly solve the problem of Chinese garbled characters in Eclipse, and attach specific code examples. one,

Effective method to solve matplotlib Chinese garbled problem Effective method to solve matplotlib Chinese garbled problem Jan 13, 2024 am 11:03 AM

An effective method to quickly solve matplotlib Chinese garbled characters. Introduction: matplotlib is a commonly used drawing library in Python. However, when using Chinese for annotation and display, garbled characters often occur. This article will introduce some effective workarounds and provide specific code examples. 1. Set the font. matplotlib uses system fonts for Chinese display by default. However, system fonts often do not contain Chinese characters, so you need to manually set the appropriate Chinese font. First, you need to confirm whether the computer has

See all articles