您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 遍历目录文件编码转换

2024/3/2 1:28:18发布12次查看
文件编码转换与字符串编码转换有一些区别了,如果我们一个目录几百个文件的编码有问题我们肯定使用工具来处理,下面分享一估php 遍历目录文件编码转换的例子。
遍历当前目录及子目录。把所有的文件转换编码到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);
            }
        }
    }
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product