How to solve php opendir garbled problem

藏色散人
Release: 2023-03-14 19:12:01
Original
1789 people have browsed it

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.

How to solve php opendir garbled problem

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)){
    //列出所有文件并去掉&#39;.&#39;和&#39;..&#39;
    if($file!=&#39;.&#39; &&$file!=&#39;..&#39;){
    //$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);
?>
Copy after login

(1) Make a conversion before output, and add the following code:

$value = iconv("UTF-8","gb2312",$value);  //或者 iconv("gb2312","UTF-8",$value);
Copy after login

(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!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template