扫描某文件夹下的所以文件 并把文件中的内容全部转码

<?php

$hostdir=dirname(__FILE__);
$filesnames = scandir($hostdir);
unset($filesnames[0]);
unset($filesnames[1]);
unset($filesnames[2]);

foreach ($filesnames as $k=>$v){
    $fileType = mb_detect_encoding($v ,  array('UTF-8','GBK','LATIN1','BIG5')) ;
    if( $fileType != 'UTF-8') {
        $contents_before = file_get_contents($v);
        $contents_after=mb_convert_encoding($contents_before ,'utf-8' , $fileType);
       // $contents_after = iconv($fileType,'UTF-8',$contents_before);
        file_put_contents($v, $contents_after);
    }
}
echo 1;

猜你喜欢

转载自blog.csdn.net/ufan94/article/details/79508297