有个需求,把文件夹下的所有HTML文件读取名字,并展示在后台页面
$_filelist = scandir(dirname(APP_PATH).'/public/doc');
foreach ($_filelist as $key => $value) {
$tmp = pathinfo($value);
if($tmp['extension'] == 'html') {
$filelist[] = array('url'=>$value, 'name'=>str_replace('.html', '', $value));
}
}
但是这种写法,在后台展示的都是乱码,考虑到读取出来的文件名不一定是UTF8,需要转换编码
转换编码函数
function strToUtf8($str){
$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
if($encode == 'UTF-8'){
return $str;
}else{
return mb_convert_encoding($str, 'UTF-8', $encode);
}
}
网友评论