在PHP中将提取的Zip文件重命名为其他语言时出错

Vee*_*ich 13 php unicode renaming ziparchive thai

我使用PHP ZipArchive类来提取.zip文件,它适用于英语,但导致我的本地语言(THAI)出现问题.

icov('utf-8','windows-874',$zip->getNameIndex($i))用来将utf-8转换为THAI.它适用于文件夹/文件的名称,但不适用于解压缩的.zip文件并导致此错误:

iconv():检测到输入字符串中的非法字符

谁能告诉我这里的问题是什么?

我的PHP代码

$file = iconv('utf-8', 'windows-874', $_GET['File']);
$path = iconv('utf-8', 'windows-874', $_GET['Path']);

$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
    // convert to Thai language
    for($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        //echo iconv("charset zip file", "windows-874", $name);
        //$zip->extractTo($path,$name); -> this problem
    }
    $zip->close();
    echo json_encode('unZip!!!');
} else {
    echo json_encode('Failed');
}
Run Code Online (Sandbox Code Playgroud)

解压缩压缩文件后,文件的名称不是我为其设置的名称. 解压缩压缩文件后,文件的名称不是我为其设置的名称.

这是我试图设置的名称: 这是我试图设置的名称:

这是我的压缩文件:

https://www.dropbox.com/s/9f4j04lkvsyuy63/test.zip?dl=0

更新
我尝试在Windows XP中解压缩文件,它在那里工作正常,但在Windows 7中没有.

Tec*_*ant 1

您可能应该尝试mb_detect_encoding()来获取帮助 - 请参阅下面的代码。如果您的路径也有问题,您可能需要扩展此代码。如果需要的话,只需使用循环即可。

$file = iconv('utf-8', 'windows-874', $_GET['File']);
$path = iconv('utf-8', 'windows-874', $_GET['Path']);

$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
    // convert to Thai language
    for($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        $order = mb_detect_order();
        $encoding = mb_detect_encoding($name, $order, true);
        if (FALSE === $encoding) {
             throw new UnexpectedValueException(
                sprintf(
                    'Unable to detect input encoding with mb_detect_encoding, order was: %s'
                , print_r($order, true)
                )
             );
        } else {
            $encoding = mb_detect_encoding($name);
            $stringUtf8 = iconv($encoding, 'UTF-8//IGNORE', $name);
            $zip->extractTo($path,$stringUtf8);
        }  
    }
    $zip->close();
    echo json_encode('unZip!!!');
} else {
    echo json_encode('Failed');
}
Run Code Online (Sandbox Code Playgroud)