PHP Can't rename files with special characters (Windows)

1.8k Views Asked by At

Looping through a folder, renaming it's files. Everything works fine, except for files with characters like ąčęėįšųū in their names (Before using mb_convert_encoding all the mentioned symbols used to appear as �)

How could this be fixed?

Code:

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>';}

        }

    }

Results:

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
2

There are 2 best solutions below

0
On

Yes it is possible with PHP to renamme a file (ie an image) on win7 containing foreign caracters, using PHP function rename - but this is very tricky

the key is: all must be done using UTF8 except the "old wrong name":

rename($srv.'\commun\'.$rp.'\'.utf8_decode($im),$srv.'\commun\'.$rp.'\'.$nm);

to read a file with foreign caracters, use PHP urlencode

0
On

To rename with special characters on Windows, you have to encode your string back to iso-8859-1 (or cp1252 which is pretty much the same). Windows won't like utf-8 strings for filenames.

mb_convert_encoding($new, 'iso-8859-1');