Home  >  Article  >  Backend Development  >  How to solve the garbled problem of php readdir

How to solve the garbled problem of php readdir

藏色散人
藏色散人Original
2021-10-20 09:31:162244browse

php Solution to readdir garbled code: 1. Modify the encoding through "mb_convert_encoding"; 2. Use the "utf8_encode" method to modify the encoding.

How to solve the garbled problem of php readdir

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to solve the php readdir garbled problem?

Specific problem:

PHP uses opendir readdir Chinese files are garbled

<?php
//打开 images 目录
$handle = opendir("images");
//列出 images 目录中的文件
while (($file = readdir($handle)) !== false){
  echo "filename: " . $file . "<br />";
}
closedir($dir);
?>

The Chinese files or Chinese directories read here are null. Why is this?

Note: You need to use ajax to request data.

Solution:

windows:

echo mb_convert_encoding ($file,&#39;UTF-8&#39;,&#39;GBK&#39;);

Linux:

echo utf8_encode($file);

Recommended learning: "PHP Video tutorial

The above is the detailed content of How to solve the garbled problem of php readdir. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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