PHP翻译和重命名文件



这是我的问题。文件未重命名。我做错了什么?我看不到什么?这个脚本必须在Windows和Unix中运行。UNIX UTF-8脚本文件,不带BOM。尝试了Windows 1251,ANSI,但仍然无法工作。

 <?php
 function Transliteration($FileName){ 
 $CharReplace = array (
'А'=>'A', 'Б'=>'B', 'В'=>'V',
'Г'=>'G', 'Д'=>'D', 'Е'=>'E',
'Ё'=>'E', 'Ж'=>'ZH', 'З'=>'Z',
'И'=>'I', 'Й'=>'J', 'К'=>'K',
'Л'=>'L', 'М'=>'M', 'Н'=>'N',
'О'=>'O', 'П'=>'P', 'Р'=>'R',
'С'=>'S', 'Т'=>'T', 'У'=>'U',
'Ф'=>'F', 'Х'=>'H', 'Ц'=>'TS',
'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHH',
'Ъ'=>'', 'Ы'=>'I', 'Ь'=>'',
'Э'=>'E', 'Ю'=>'YU', 'Я'=>'YA',
'а'=>'a', 'б'=>'b', 'в'=>'v',
'г'=>'g', 'д'=>'d', 'е'=>'e',
'ё'=>'yo', 'ж'=>'zh', 'з'=>'z',
'и'=>'i', 'й'=>'j', 'к'=>'k',
'л'=>'l', 'м'=>'m', 'н'=>'n',
'о'=>'o', 'п'=>'p', 'р'=>'r',
'с'=>'s', 'т'=>'t', 'у'=>'u',
'ф'=>'f', 'х'=>'h', 'ц'=>'ts',
'ч'=>'ch',  'ш'=>'sh', 'щ'=>'shh',
'ъ'=>'', 'ы'=>'i', 'ь'=>'',
'э'=>'e', 'ю'=>'yu', 'я'=>'ya',
"№"=>"N", " "=>"_", "–"=>"_",
"-"=>"_", " - "=>"_", ","=>"");
$FileNameTranslited = str_replace(array_keys($CharReplace), $CharReplace, $FileName);
return $FileNameTranslited;}
function Renaming(){
$WorkDir = opendir("ToRename") or die("Не могу открыть папку");
while ($CurrentFile = readdir($WorkDir)){
    if ($CurrentFile != "." && $CurrentFile != ".."){
        $TranslitedFile = Transliteration($CurrentFile);
        if (rename($CurrentFile, $TranslitedFile))
            {echo "File Renamed";}
            else{echo "Some shit happen!";}
        echo $CurrentFile." -> ".$TranslitedFile."<br>";}}}
 Renaming();
 ?>

非常感谢StathisG!这是解决方案的正确关键。但它仍然不起作用。看这里:

 function Renaming(){
 $directory = 'ToRename/';
 $WorkDir = opendir($directory) or die("Не могу открыть папку");
 while ($CurrentFile = readdir($WorkDir)){
   if ($CurrentFile != "." && $CurrentFile != ".."){
    $WhichCodingWeWant = 'UTF-8';
    $FileNameCoding = mb_detect_encoding($CurrentFile);
    echo $FileNameCoding."<br/>";
    $utf8_filename = mb_convert_encoding($CurrentFile, $WhichCodingWeWant, $FileNameCoding);
    $TranslitedFile = Transliteration($utf8_filename);
    mb_convert_encoding($TranslitedFile, $FileNameCoding, $WhichCodingWeWant);
    echo mb_detect_encoding($TranslitedFile)."<br/>";
    if (rename($directory . $CurrentFile, $directory . $TranslitedFile)) {
       echo "File Renamed<br/>";
       } else {
         echo "Some shit happen!<br/>";
          }
        echo $utf8_filename." -> ".$TranslitedFile."<br>";
       }
    }
 }
   Renaming(); 

正如你所看到的,我添加了一个新的变量"$WhichCodingWeWant"one_answers"$FileNameCoding"。传入文件名:"Новыйриекс我的大脑爆炸了。。。


好的。。。步骤3。传入数据如前所述:

 function Renaming(){
 $directory = 'ToRename/';
 $WorkDir = opendir($directory) or die("Не могу открыть папку");
 while ($CurrentFile = readdir($WorkDir)){
    if ($CurrentFile != "." && $CurrentFile != ".."){
        echo "What name is come: ".$CurrentFile."<br/>";
        $WhichCodingWeWant = 'UTF-8';
        $FileNameCoding = mb_detect_encoding($CurrentFile);
        echo "File name encoding: ".$FileNameCoding."<br/>";
        $utf8_filename = mb_convert_encoding($CurrentFile, $WhichCodingWeWant, $FileNameCoding);
        echo "File name behind transliting: ".$utf8_filename."<br/>";
        $TranslitedFile = Transliteration($utf8_filename);
        echo "File name translited to: ".$TranslitedFile."<br/>";
        mb_convert_encoding($TranslitedFile, $FileNameCoding, $WhichCodingWeWant);
        echo "File name encoding converted to: ".mb_detect_encoding($TranslitedFile)."<br/>";
        if (rename($directory . $CurrentFile, $directory . $TranslitedFile)) {
            echo "File Renamed<br/>";
        } else {
            echo "Some shit happen!<br/>";
        }
        echo $utf8_filename." -> ".$TranslitedFile."<br>";
    }
 }
 }
 Renaming();
 Result is: 
 What name is come: Новый текстовый документ.txt
 File name encoding: UTF-8
 File name behind transliting: ????? ????????? ????????.txt
 File name translited to: ?????_?????????_????????.txt
 File name encoding converted to: ASCII

警告::第32行的E:\WEB\XAMPP\htdocs\my\Site\test\test6.php中没有错误有些事情发生了!????????。txt->????????。txt并且文件不会在文件夹中重命名。

如果我想要并制作UTF-8,为什么选择ASCII?我明白,一个我什么都不懂!无论如何,谢谢StathisG帮我!明天我将在Linux系统中试用这个脚本。告诉你结果。如果你对这一切有一些想法,我很高兴看到:(

您的代码会产生以下警告:

警告:重命名(test.txt,test.txt(:系统找不到文件指定。

$CurrentFile变量只包含文件名,而不包含文件的完整路径。尝试以下操作:

function Renaming(){
    $directory = 'ToRename/';
    $WorkDir = opendir($directory) or die("Не могу открыть папку");
    while ($CurrentFile = readdir($WorkDir)){
        if ($CurrentFile != "." && $CurrentFile != ".."){
            $utf8_filename = mb_convert_encoding($CurrentFile, 'UTF-8', 'GREEK');
            $TranslitedFile = Transliteration($utf8_filename);
            if (rename($directory . $CurrentFile, $directory . $TranslitedFile)) {
                echo "File Renamed";
            } else {
                echo "Some shit happen!";
            }
            echo $utf8_filename." -> ".$TranslitedFile."<br>";
        }
    }
}
Renaming();

我单独测试了Transliteration变量,它似乎运行良好(请参阅下面的测试(,所以忽略我关于多字节字符串函数的原始注释。

echo Transliteration('Не могу открыть папку'); // produces 'Ne_mogu_otkrit_papku'

编辑:

我编辑了上面的代码,添加了以下行:

$utf8_filename = mb_convert_encoding($CurrentFile, 'UTF-8', 'GREEK');

然后,我使用$utf8_filename作为传递给Transliteration函数的变量:

$TranslitedFile = Transliteration($utf8_filename);

正如你可能注意到的,我使用了"GREEK"作为文件名的编码,因为这是我除了英语之外唯一知道的语言,所以我使用希腊语文件名来测试你的代码。

我创建了一个名为"τεστ.txt"的文件,并将以下值添加到$CharReplace数组:'τ'=>'t', 'ε'=>'e', 'σ'=>'s'

当我运行代码时,我得到了以下消息,文件被成功重命名为"test.txt">

File Renamed τεστ.txt -> test.txt

根据PHP手册,mb_convert_encoding支持的编码如下。

因此,请尝试上面的代码,将编码值替换为与您使用的字符相对应的编码,并检查这是否解决了您的问题。

最新更新