如何更改文件名的编码类型?

问题描述 投票:1回答:1

我要将我的文件名的编码类型从utf-8更改为big5,这是我到目前为止所做的:

$path = "stu_resume/104206002_87";
$result =iconv("utf-8", "big5", $path);
echo $result;
echo mb_detect_encoding($result);

在104206002_87的文件夹中,有2个文件,即104206002_87_履历,104206002_87_自传。 执行上面的代码后,我发现文件夹中没有任何变化。有谁知道如何解决这个问题?非常感谢。

php file encode
1个回答
0
投票

iconv()不会修改文件。它只是转换一个字符串。在这种情况下,它转换的字符串是""stu_resume/104206002_87" - 因为这个字符串只包含ASCII字符,所以当它从UTF-8转换为Big5时没有任何变化。

如果要重命名具有该名称的目录中的文件,则需要明确地执行此操作,例如:

$iter = new DirectoryIterator("stu_resume/104206002_87");
foreach ($iter as $file) {
    if (!$file->isDot()) {
        $old_name = $file->getPathname();
        $new_name = iconv("utf-8", "big5", $old_name);
        rename($old_name, $new_name);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.