Table of Contents
回复讨论(解决方案)

TP小问题

Jun 20, 2016 pm 12:29 PM

使用TP遍历Public文件夹下某个目录的所有文件名,测试了下,发现中文文件名都显示乱码,
文件都是UTF-8编码的。原生PHP测试中使用没有这个问题。特来请教。
以下是TP中代码部分:

 public function index(){        $dir="./Public/Articles"; //文章目录        $data=$this->dirList($dir);        $arr=array();        foreach($data as $v){            $arr[]=$v;        }       print_r($arr); //中文乱码       $this->ajaxReturn($arr);    }    public function dirList($dir){            $arr=scandir($dir);            foreach($arr as $val){                if($val!='.' && $val!='..'){                    $arr2[]=$val;                }            }        return $arr2;    }
Copy after login

如图:


回复讨论(解决方案)

window 系统的中文文件名都是 gbk 编码的

您认为是小问题吗?非也!
如果连赖以生存的环境都不甚了了,不出大问题才怪呢

您认为是小问题吗?非也!
如果连赖以生存的环境都不甚了了,不出大问题才怪呢


就算我把文件改成GBK编码,也是一样的结果。原生PHP是正常的,不会乱码。所以不知道是怎么回事。
另外吐槽下,PHP打开一个中文文本,还要转个编码,实在是太土了。

在ajaxReturn前设置header头呢?
header('Content-type:application/json;charset=utf-8');

在ajaxReturn前设置header头呢?
header('Content-type:application/json;charset=utf-8');


不可以。我后来加了
        header('Content-type:text/html;charset=gbk');

倒是可以打印出中文,但是ajax返回的data那边显示出来就是NULL,实在郁闷了。


在ajaxReturn前设置header头呢?
header('Content-type:application/json;charset=utf-8');


不可以。我后来加了
        header('Content-type:text/html;charset=gbk');

倒是可以打印出中文,但是ajax返回的data那边显示出来就是NULL,实在郁闷了。
header('Content-type:tex/html;charset=gbk');
ajaxReturn函数默认使用json格式输出,你在header声明为text/html,返回肯定为null啊,改为application/json试试行不行

1、linux 系统的默认字符集是 utf-8
2、简体中文 windows 系统的默认字符集是 gbk
3、TP 的工作字符集是 utf-8

所以 TP 在 linux 中不会有问题
但在 windows 中就会有乱码问题

所以你在读取文件名后,要根据操作系统做字符集转换

php 的 json 函数只支持 utf-8 字符集

PHP打开一个中文文本,还要转个编码,实在是太土了
这只是你自己再给你自己找麻烦

1、linux 系统的默认字符集是 utf-8
2、简体中文 windows 系统的默认字符集是 gbk
3、TP 的工作字符集是 utf-8

所以 TP 在 linux 中不会有问题
但在 windows 中就会有乱码问题

所以你在读取文件名后,要根据操作系统做字符集转换

php 的 json 函数只支持 utf-8 字符集

PHP打开一个中文文本,还要转个编码,实在是太土了
这只是你自己再给你自己找麻烦


我的文本不管是GBK编码还是UTF-8,返正都是乱码。ajax那边接受的文本名显示NULL。
而PHP打开 中文名字的文本,如果不转个编码打开,就是乱码,还有什么好方式么?

在同一环境中显示不同编码的数据,总是要有乱码出现的

windows下的文件名需要执行iconv('GBK','UTF-8',$name); 才能utf8编码输出。

windows读取中文文件名就是要转编码
最好的方式就是换linux,比如windows上装个linux虚拟机什么的
不然就封一个读文件的函数,读文件统一用那个函数,函数里写转换

windows下的文件名需要执行iconv('GBK','UTF-8',$name); 才能utf8编码输出。


路径中带有中文也是这么转换么?试了下好像还是乱码。
$data=$this->dirList($dir);
打印出data必须要转成GBK才能正常显示,UTF8不行。然后在ajax那边返回值就是NULL之类的。

windows读取中文文件名就是要转编码
最好的方式就是换linux,比如windows上装个linux虚拟机什么的
不然就封一个读文件的函数,读文件统一用那个函数,函数里写转换



转了编码也不成功,能指点下么?
$data=$this->dirList($dir); 这里
打印出data必须要加上    header('Content-type:text/html;charset=gbk');
才能在浏览器正常显示,UTF8不行。然后在ajax那边返回值就是NULL之类的。

gbk->utf8
$file = mb_convert_encoding($file, 'utf-8', 'UTF-8,GBK,ASCII');
utf8->gbk
$file = iconv('UTF-8', 'GBK//IGNORE', $file);

gbk->utf8
$file = mb_convert_encoding($file, 'utf-8', 'UTF-8,GBK,ASCII');
utf8->gbk
$file = iconv('UTF-8', 'GBK//IGNORE', $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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

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

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles