Home  >  Article  >  Backend Development  >  Implementation code for batch replacement of file names in php

Implementation code for batch replacement of file names in php

高洛峰
高洛峰Original
2016-11-30 11:36:071489browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn