Home > Article > Backend Development > Implementation code for batch replacement of file names in php
The code is as follows:
$dir = 'D:Program FilesresourceapplicationSkinPNG\';//Pay attention to the path here, add two at the end, the first one represents the escape, but it is easy to encounter other specific escapes, so you need to judge carefully , can be written as follows
$dir = 'D:/Program Files/resource/application/Skin/PNG/';//Write such a path, you don't have to worry about escaping. Don’t omit the last/don’t write
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if ($file != ". " && $file != "..")
{
if(filetype($dir . $file) == 'file')
{
$newfile = str_replace(array('_PNG','_XML','_ICO '),array('.PNG','.XML','.ICO'), $file);
var_dump($file.' =======> '.$newfile.'
');
rename($dir . $file, $dir . $newfile);
}
}
}
closedir($dh);
}