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.
PHP 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!='.' && $fl != '..' 则会造成把$dir的父级目录也读取出来 if(is_dir($temp) && $fl!='.' && $fl != '..'){ echo '目录:'.$temp.'<br>'; read_all($temp); }else{ if($fl!='.' && $fl != '..'){ echo '文件:'.$temp.'<br>'; } } } } } read_all('D:\wamp\www\test'); ?>
Open the browser with confidence
<?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('GBK','utf-8',$dir.DIRECTORY_SEPARATOR.$fl);//转换成utf-8格式 //如果不加 $fl!='.' && $fl != '..' 则会造成把$dir的父级目录也读取出来 if(is_dir($temp) && $fl!='.' && $fl != '..'){ echo '目录:'.$temp.'<br>'; read_all($temp); }else{ if($fl!='.' && $fl != '..'){ echo '文件:'.$temp.'<br>'; } } } } } read_all('D:\wamp\www\test'); ?>
file by is_dir! The following file has not been read!
I tested it alone<?php $dir='D:\wamp\www\test\test_dir\子目录'; var_dump(is_dir($dir)); ?>
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!