1

遍历文件夹,重命名它的文件。一切正常,除了名称中带有 ąčęėįšųū 等字符的文件(在使用 mb_convert_encoding 之前,所有提到的符号都显示为 �)

这怎么可能解决?

代码:

function ltu($str){
        $lietuviskos = array('ą', 'Ą', 'č','Č', 'ę','Ę',  'ė','Ė',  'į','Į',  'š','Š',  'ų','Ų',  'ū','Ū',  'ž','Ž', '%', ',', '(' , ')', '+');
        $lotyniskos = array('a', 'a', 'c', 'c','e','e', 'e','e', 'i','i', 's','s', 'u','u', 'u','u', 'z','z','','_','','','');
        return str_replace($lietuviskos, $lotyniskos, $str);
    }

    $base = "../../foto/";

    $manufacturers = scandir($base);
    unset($manufacturers[0]);
    unset($manufacturers[1]);

    foreach ($manufacturers as $key => $manufacturer) {
        $files = scandir($base.$manufacturer);
        unset($files[0]);
        unset($files[1]);

        foreach ($files as $key => $file) {
            $file = mb_convert_encoding($file, 'UTF-8', 'ISO-8859-13');

            $goodFile = ltu($file);

            $oldFileUrl = 'D:/foto/'.$manufacturer.'/'.$file;
            $goodFileUrl = 'D:/foto/'.$manufacturer.'/'.$goodFile;

            try{
               if(rename($oldFileUrl,$goodFileUrl)){
                  echo 'success on: '.$oldFileUrl.'<br>';
               } else {
                  throw new Exception('Can not rename file'.$oldFileUrl);
               }
            }catch (Exception $e){echo 'FAIL on '.$oldFileUrl.'<br>';}

        }

    }

结果:

success on: D:/foto/3M ESPE/1806 - Filtek Ultimate paplidymas.jpg
success on: D:/foto/3M ESPE/1842 - Filtek Ultimate 3920P rinkinys 12x4g.jpg
success on: D:/foto/3M ESPE/1873 - Ketac Fil Plus 55291 standartinis rinkinys.jpg
success on: D:/foto/3M ESPE/2003 - Protemp Crown.jpg
success on: D:/foto/3M ESPE/2072 - Relyx fiber post.jpg
success on: D:/foto/3M ESPE/2316 - Filtek Z250 6020TK rinkinys.jpg
success on: D:/foto/3M ESPE/2316 - Filtek Z250 7050TK rinkinys 4x4g.jpg
FAIL on D:/foto/3M ESPE/289 - Filtek Z250 6020A1 papildymas.gif
FAIL on D:/foto/3M ESPE/2909 - Ketac-Cem 37230RE radiopaque cementras triguba pakuotė.jpg
success on: D:/foto/3M ESPE/3025 - Impregum Penta H duosoft RF 31740.jpg
FAIL on D:/foto/3M ESPE/303 - Filtek P60 4720E įvadinis rinkinys.jpg
success on: D:/foto/3M ESPE/314 - Adper single bond 2 51202.png
success on: D:/foto/3M ESPE/316 - Ketac-bond 37330 milteliai.jpg
FAIL on D:/foto/3M ESPE/317 - Ketac_bond 37390 chem. kietėjimo pamušalas.jpg
FAIL on D:/foto/3M ESPE/329 - Ketac-Cem karūnėlių cement. 33g12ml.jpg
success on: D:/foto/3M ESPE/330 - Ketac-Cem 37210 milteliai 33g.jpg
4

2 回答 2

0

是的,使用 PHP 可以使用 PHP 函数重命名在 win7 上重命名包含外国字符的文件(即图像) - 但这非常棘手

关键是:除了“旧的错误名称”之外,所有都必须使用 UTF8 完成:

重命名($srv.'\commun\'.$rp.'\'.utf8_decode($im),$srv.'\commun\'.$rp.'\'.$nm);

要读取带有外来字符的文件,请使用 PHP urlencode

于 2014-12-21T20:25:49.800 回答
0

要在 Windows 上使用特殊字符重命名,您必须将字符串编码回iso-8859-1(或几乎相同的cp1252)。Windows 不喜欢文件名的 utf-8字符串。

mb_convert_encoding($new, 'iso-8859-1');
于 2017-04-12T23:15:08.160 回答