遍历当前目录及子目录。把所有的文件转换编码到utf-8
代码如下 复制代码
//php iconv.php
//exec it on root dir
$path = dirname(__file__);
tree($path);
function encodefiles($filename)
{
// echo $filename;
if (file_exists($filename)) {
// read in the contents
$res = file_get_contents($filename);
$i = pathinfo($filename);
if(!in_array($i['extension'],array('js','css','php','html','htm'))){
return ;
}
// just display on the screen the file being modified
echo $filename . ---form---;
// convert the contents
echo $encode = mb_detect_encoding()($res, array(ascii, utf-8, gb2312, gbk));
echo ---to---utf-8!\n;
$res = iconv($encode, utf-8, $res);
// write back out to the same file
file_put_contents($filename, $res);
} //
}
function tree($directory)
{
$mydir = dir($directory);
while ($file = $mydir->read()) {
if ((is_dir($directory/$file)) and ($file != .) and ($file != ..)) {
tree($directory/$file);
} else {
$file =$directory/$file;
// echo
$file\n;
if(is_file($file)){
encodefiles($file);
}
}
}
}
