遍历文件夹,重命名它的文件。一切正常,除了名称中带有 ąčęėįšųū 等字符的文件(在使用 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