Home >Backend Development >PHP Problem >How to batch modify file names in php

How to batch modify file names in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-28 15:23:222905browse

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

How to batch modify file names in php

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

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

需求描述:

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

代码实现:

方法一

<?php
$dir = __DIR__."\image\\";
$list = scandir($dir);
foreach ($list as $item) {
  if(!in_array($item,[&#39;.&#39;,&#39;..&#39;])){
    $arr = explode(".", $item);
    $origin_name = reset($arr);
    $new_name = $origin_name.&#39;Abc.&#39;.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,[&#39;.&#39;,&#39;..&#39;])){
    $arr = explode(".", $item);
    $origin_name = reset($arr);
    $new_name = $origin_name.&#39;Abc.&#39;.end($arr);
    $origin_path = $dir.$item;
    $new_path = $dir.$new_name;
    copy($origin_path, $new_path);
    unlink($origin_path);
  }
}

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

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

The above is the detailed content of How to batch modify file names in php. For more information, please follow other related articles on the PHP Chinese website!

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