1:
<?php
2:
// +--------------------------------------------+
3:
// | Döp om filer i en mapp till "giltiga" namn |
4:
// +--------------------------------------------+
5:
// | Av David Runemalm 2005 |
6:
// +--------------------------------------------+
7:
8:
function modifyName($file) {
9:
$filename = $file;
10:
11:
//Ändra $pattern och $replace nedan till de teckenbyten du vill göra
12:
$pattern = array ('/\s/', '/å/', '/ä/' ,'/ö/' ,'/Å/' ,'/Ä/' ,'/Ö/', '/JPG/');
13:
$replace = array ('/\s/' => '_', '/å/' => 'a', '/ä/' => 'a', '/ö/' => 'o',
14:
'/Å/' => 'A', '/Ä/' => 'A', '/Ö/' => 'O', '/JPG/' => 'jpg');
15:
16:
foreach($pattern as $value) {
17:
$filename = preg_replace($value, $replace[$value], $filename);
18:
}
19:
return $filename;
20:
}
21:
22:
//Sökväg till mapp - OBS! Byt till rätt mapp
23:
$dir = "path/to/dir";
24:
25:
//Döp om filerna
26:
if ($handle = opendir($dir)) {
27:
while (false !== ($file = readdir($handle))) {
28:
if ($file != "." && $file != "..") {
29:
$old_name = $dir. "/" . $file;
30:
$new_name = $dir. "/" . modifyName($file);
31:
if (rename($old_name, $new_name)) {
32:
echo "$new_name<BR>";
33:
} else { //Ifall filen inte byter namn
34:
echo "--------------------ERROR-------------------- Filen $old_name bytte inte namn<BR>";
35:
}
36:
37:
}
38:
}
39:
}
40:
41:
//Stäng mappen
42:
closedir($handle);
43:
?>