Home Backend Development PHP Tutorial What to do if php files are uploaded with Chinese garbled characters, and the problem with Chinese garbled characters in php file names

What to do if php files are uploaded with Chinese garbled characters, and the problem with Chinese garbled characters in php file names

Jul 25, 2016 am 08:52 AM

  1. enctype="multipart/form-data">

复制代码

php上传代码:

  1. if ($_FILES["file"]["error"] > 0)
  2. {
  3. echo "Return Code: " . $_FILES["file"]["error"] . "
    ";
  4. }else
  5. {
  6. echo "Upload: " . $_FILES["file"]["name"] . "
    ";
  7. echo "Type: " . $_FILES["file"]["type"] . "
    ";
  8. echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
    ";
  9. echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
    ";
  10. if (file_exists("upload/" . $_FILES["file"]["name"]))
  11. {
  12. echo $_FILES["file"]["name"] . " already exists. ";
  13. }
  14. else
  15. {
  16. move_uploaded_file($_FILES["file"]["tmp_name"],
  17. "upload/" . $_FILES["file"]["name"]);
  18. }
  19. }
复制代码

上传了一个文件名为“测试数据.txt”的文件,oh ho,文件是传上去了,但是文件名为乱码。

以下是php文件上传乱码的原因分析,并给出了一些可靠的解决方法。

将:

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

修改为:

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

结果发现iconv函数返回值为false。 查一下函数手册,发现第二个参数有特别的用法,简单翻译一下就是我可以在编码的后面追加//TRANSLIT 或 //IGNORE ,前者会将无法翻译的字符转成最接近的字符,后者就是直接忽略不能转化的字符。

如下图: What to do if php files are uploaded with Chinese garbled characters, and the problem with Chinese garbled characters in php file names

测试:

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

结果: bool(false) string(4) ".txt" 也就是说中文都没法转化,甚至连接近的字符都没有,看来网上介绍的方法也并非万能。

3、网上介绍方法失败,再尝试 猜测一下,也许系统在创建中文文件的时候会乱码,修改代码:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/测试数据.txt");

结果创建成功,没有乱码。不是系统问题。

php文件本身是utf8编码的,那么

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/测试数据.txt");

这个语句肯定使用的是utf8编码,那么之前上传的文件名肯定就不是utf8编码了,那么以下的语句肯定是错误的,因为源字符串本身就不是utf8编码的:

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

使用函数检查源字符串的编码:

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

结果是CP936,也就是源字符串编码是GBK。

测试:

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

问题解决,不再有中文乱码问题了。

4、其它解决方法,在html文件的head标签中间加入:

从而使编码保持统一,也就不需要再转码了。

5、小结 使用iconv函数可以解决上传中文文件名乱码的问题,实际上iconv能解决各种各样的由于编码不统一造成的乱码问题。 使用iconv函数请先检查源字符串的编码,除非你已经确定了源字符串的编码。 尽量保证所有的代码的编码一致,万不得已才使用iconv函数。

尽量不使用中文文件名作为服务器上保存的文件名,请将文件名转化成自己的文件名(即使是英文文件名也请转化一下)。



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles