您的位置:首页 > 技术中心 > PHP教程 >

php怎样批量修改文件名

时间:2020-08-28 16:02

php批量修改文件名的方法:首先打开编辑器;然后使用函数item替换函数名,代码为【if(!in_array($item,['.','..']))$arr = explode(".", $item);】。

相关学习推荐:php图文教程

php批量修改文件名的方法:

需求描述:

某个文件夹下有100个文件,现在需要将这个100个文件的文件名后添加字符串Abc(后缀名保持不变)。

代码实现:

方法一

<?php
$dir = __DIR__."\image\\";
$list = scandir($dir);
foreach ($list as $item) {
  if(!in_array($item,['.','..'])){
    $arr = explode(".", $item);
    $origin_name = reset($arr);
    $new_name = $origin_name.'Abc.'.end($arr);
    $origin_path = $dir.$item;
    $data = file_get_contents($origin_path);
    $new_path = $dir.$new_name;
    $res[] = file_put_contents($new_path, $data);
    unlink($origin_path);
  }
}

方法二

<?php
$dir = __DIR__."\image\\";
$list = scandir($dir);
foreach ($list as $item) {
  if(!in_array($item,['.','..'])){
    $arr = explode(".", $item);
    $origin_name = reset($arr);
    $new_name = $origin_name.'Abc.'.end($arr);
    $origin_path = $dir.$item;
    $new_path = $dir.$new_name;
    copy($origin_path, $new_path);
    unlink($origin_path);
  }
}

方法二使用了copy函数,更加简便。

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

以上就是php怎样批量修改文件名的详细内容,更多请关注www.gxlsystem.com其它相关文章!

本类排行

今日推荐

热门手游