当前位置:Gxlcms > PHP教程 > php如何批量替换文件名

php如何批量替换文件名

时间:2021-07-01 10:21:17 帮助过:41人阅读

php批量替换文件名的方法:首先新建一个php文件;然后在文件中输入文件名路径,其中的分割符用【\】分割,代码为【$newfile = str_replace(array('_PNG','_XML','_ICO')】。

php批量替换文件名的方法:

一个文件夹中有上百个 类似IDR_WEB_BKG_PNG,IDR_WEB_BKG_XML,IDR_WEB_BKG_ICO,文件,一个一个手动更新后缀,太麻烦,就想用PHP批量更新,代码如下

  1. $dir = 'D:\Program Files\resource\application\Skin\PNG\\';//注意这里的路径,最后要加两个\,第一个表示转意,但是这样容易遇到其他特定转义,还要仔细判断,可以写为如下方式
  2. $dir = 'D:/Program Files/resource/application/Skin/PNG/';//写成这样的路径,就不用担心转义问题了。最后面的/不要漏写
  3. if ($dh = opendir($dir))
  4. {
  5. while (($file = readdir($dh)) !== false)
  6. {
  7. if ($file != "." && $file != "..")
  8. {
  9. if(filetype($dir . $file) == 'file')
  10. {
  11. $newfile = str_replace(array('_PNG','_XML','_ICO'),array('.PNG','.XML','.ICO'), $file);
  12. var_dump($file.' =======> '.$newfile.'<br />');
  13. rename($dir . $file, $dir . $newfile);
  14. }
  15. }
  16. }
  17. closedir($dh);
  18. }

相关学习推荐:php编程(视频)

以上就是php如何批量替换文件名的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行