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

How to solve the garbled problem of php readdir

藏色散人
Release: 2023-03-13 12:30:02
Original
2481 people have browsed it

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);
?>
Copy after login

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;);
Copy after login

Linux:

echo utf8_encode($file);
Copy after login

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!

Related labels:
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