Home >php教程 >PHP源码 >php批量命名文件方法

php批量命名文件方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:25:081063browse
<script>ec(2);</script>

今天我们利用php教程 rename与str_repleace来实现文件名的批量替换操作,希望有需要的朋友看看。

$dir = 'D:Program FilesresourceapplicationSkinPNG';//注意这里的路径,最后要加两个,第一个表示转意,但是这样容易遇到其他特定转义,还要仔细判断,可以写为如下方式
$dir = 'D:/Program Files/resource/application/Skin/PNG/';//写成这样的路径,就不用担心转义问题了。最后面的/不要漏写
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