>백엔드 개발 >PHP 문제 >PHP에서 파일 이름을 일괄 수정하는 방법

PHP에서 파일 이름을 일괄 수정하는 방법

coldplay.xixi
coldplay.xixi원래의
2020-08-28 15:23:223051검색

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

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编程(视频)

위 내용은 PHP에서 파일 이름을 일괄 수정하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.