search
Homephp教程PHP源码PHP 批量修改文件名/文件后缀名(扩展名)

今天我有几千个文件要一次修改文件或把后缀名给改了,但是我要手工一个个去改,不得改几天后来想到一个办法,利用php写一个量修改文件名/文件后缀名程序,一下就实现了,下面来看看方法。

<script>ec(2);</script>

几个关键的函数。

is_dir($dirname)      //判断一个文件名是否 为目录
opendir($dirname)      //打开一个文件夹
readdir($dir_stream)   //函数返回由 opendir() 打开的目录句柄中的条目
pathinfo($path)        //函数以数组的形式返回文件路径的信息。
例如当前文件夹下的P1010436


例如当前文件夹下的P1010436.jpg文件路径信息数组:

Array
(
    [dirname] => .
    [basename] => P1010436.jpg
    [extension] => jpg
    [filename] => P1010436
)
rename(oldname,newname,context) //函数重命名文件或目录。若成功,则该函数返回 true。若失败,则返回 false。

 代码如下 复制代码

 

function fileRename($dir,$srcExtension,$desExtension){
 if(!is_dir($dir)){
  echo "{$dir}不是一个有效的目录!n";
  exit();  
  }
 $handler = opendir($dir);
 //列出$dir目录中的所有文件
 while(($fileName = readdir($handler))!=false){
  if($fileName!='.'&&$fileName!='..'){
   //'.' 和 '..'是分别指向当前目录和上级目录
   $curDir = $dir.'/'.$fileName;
   if(is_dir($curDir)){
    //如果是目录,则递归下去
    fileRename($curDir,$srcExtension,$desExtension);
    }
    else{
     //获取文件路径的信息
     $path = pathinfo($curDir);
     //print_r($path);
     if($path['extension']==$srcExtension){  
      $newname = $path['dirname'].'/'
      .$path['filename'].".".$desExtension;
      rename($curDir,$newname);   
      echo $curDir.'-->'.$newname."n";   
      }
     }
   }
  }
 }
fileRename(".","JPG","jpg");
?>


使用方法很简单,我们把图片与php文件同一目录然后再运行php即可。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool