How to solve the problem of garbled characters in php file traversal

藏色散人
Release: 2023-03-04 13:52:01
Original
2352 people have browsed it

The solution to php traversing garbled files: first create a PHP sample file; then change the file encoding to "GBK"; finally, use the "read_all" method to traverse all files in the folder.

How to solve the problem of garbled characters in php file traversalPHP Video Tutorial"

I was deeply hit by the recent interview and returned to my hometown in Henan for more than a year After giving birth to a baby, I found that I could no longer keep up with the development of the IT industry

During the interview process, I found that this question was included in the written examination questions of many companies, and it is also a function that is often used at work.

Isn’t this a piece of cake?

<?php
/*
* 遍历文件夹下所有文件
*
* 作者:郭猛
* 邮箱:martin.guo@qq.com
*
*/

function read_all ($dir){
    if(!is_dir($dir)) return false;

    $handle = opendir($dir);

    if($handle){
        while(($fl = readdir($handle)) !== false){
            $temp = $dir.DIRECTORY_SEPARATOR.$fl;
            //如果不加  $fl!=&#39;.&#39; && $fl != &#39;..&#39;  则会造成把$dir的父级目录也读取出来
            if(is_dir($temp) && $fl!=&#39;.&#39; && $fl != &#39;..&#39;){
                echo &#39;目录:&#39;.$temp.&#39;<br>&#39;;
                read_all($temp);
            }else{
                if($fl!=&#39;.&#39; && $fl != &#39;..&#39;){

                    echo &#39;文件:&#39;.$temp.&#39;<br>&#39;;
                }
            }
        }
    }
}

read_all(&#39;D:\wamp\www\test&#39;);

?>
Copy after login

Open the browser with confidence

How to solve the problem of garbled characters in php file traversal##我屮艸芔茻, what the hell! <p></p>In the windows environment, the file encoding is UTF8. When there is a Chinese directory in the directory, garbled characters will be displayed! ! <p></p>At this time, my file encoding and browser display encoding are both utf-8. I thought that the directory name of windows was GBK, so I used iconv to convert the encoding format. I changed the code as follows. Line 17:<p><div class=

<?php
/*
* 遍历文件夹下所有文件
*
* 作者:郭猛
* 邮箱:martin.guo@qq.com
*
*/

function read_all ($dir){
    if(!is_dir($dir)) return false;

    $handle = opendir($dir);

    if($handle){
        while(($fl = readdir($handle)) !== false){
            $temp = iconv(&#39;GBK&#39;,&#39;utf-8&#39;,$dir.DIRECTORY_SEPARATOR.$fl);//转换成utf-8格式
            //如果不加  $fl!=&#39;.&#39; && $fl != &#39;..&#39;  则会造成把$dir的父级目录也读取出来
            if(is_dir($temp) && $fl!=&#39;.&#39; && $fl != &#39;..&#39;){
                echo &#39;目录:&#39;.$temp.&#39;<br>&#39;;
                read_all($temp);
            }else{
                if($fl!=&#39;.&#39; && $fl != &#39;..&#39;){

                    echo &#39;文件:&#39;.$temp.&#39;<br>&#39;;
                }
            }
        }
    }
}

read_all(&#39;D:\wamp\www\test&#39;);

?>
Copy after login

Add iconv to the 17th line of code, it must be ok this time

How to solve the problem of garbled characters in php file traversalfile by is_dir! The following file has not been read!

I tested it alone

<?php

$dir=&#39;D:\wamp\www\test\test_dir\子目录&#39;;
var_dump(is_dir($dir));

?>
Copy after login
It really doesn’t work in the utf8 encoding format.

How to solve the problem of garbled characters in php file traversalJust change the file encoding to GBK. <p></p><p><img style=

The above is the detailed content of How to solve the problem of garbled characters in php file traversal. 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template