Home > Article > Backend Development > How to solve php opendir garbled problem
Solution to php opendir garbled code: 1. Open the corresponding PHP code file; 2. Execute the "$value=iconv("UTF-8","gb2312",$value);" statement before output ;3. Unify all file encodings to UTF-8 or gb2312.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to solve the php opendir garbled problem?
Solution to garbled Chinese directory reading by php
Not long ago there was a project involving using php to read file directories. Reading file directories itself is nothing. problem, but when reading the Chinese directory, it turned out to be garbled. I didn’t know what to do for a while. I haven’t encountered this problem for a long time. After some research, I came up with several solutions. I will list them now (will use The method of reading the file directory in php is also attached):
<?php //要读取的目录 $folder="D:/www"; //打开目录 $fp=opendir($folder); //阅读目录 while(false!=$file=readdir($fp)){ //列出所有文件并去掉'.'和'..' if($file!='.' &&$file!='..'){ //$file="$folder/$file"; $file="$file"; //赋值给数组 $arr_file[]=$file; } } //输出结果 if(is_array($arr_file)){ while(list($key,$value)=each($arr_file)){ echo "$key=>$value<br>"; } } //关闭目录 closedir($fp); ?>
(1) Make a conversion before output, and add the following code:
$value = iconv("UTF-8","gb2312",$value); //或者 iconv("gb2312","UTF-8",$value);
(2) All file encodings are Unify to UTF-8 or gb2312
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve php opendir garbled problem. For more information, please follow other related articles on the PHP Chinese website!