Home > php教程 > php手册 > php批量修改文件名程序

php批量修改文件名程序

WBOY
Release: 2016-06-13 10:00:31
Original
759 people have browsed it

提供二款利用遍历批量修改文件的文件名的php代码,有需要的朋友可以参考一下。

实例一

 代码如下 复制代码
//利用PHP目录和文件函数遍历用户给出目录的所有的文件和文件夹,修改文件名称
function fRename($dirname){
 if(!is_dir($dirname)){
  echo "{$dirname}不是一个有效的目录!";
  exit();
 }
 $handle = opendir($dirname);
 while(($fn = readdir($handle))!==false){
  if($fn!='.'&&$fn!='..'){
   $curDir = $dirname.'/'.$fn;
   if(is_dir($curDir)){
    fRename($curDir);
   }
   else{
    $path = pathinfo($curDir);
    $newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];
    rename($curDir,$newname);   
    echo $curDir.'---'.$newname."
";   
   }
  }
 }
}
//给出一个目录名称调用函数
fRename('pl');
?>

实例二

 代码如下 复制代码

$dir = './';

if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
        if ($file == "." || $file == "..") continue;
        if(filetype($dir . $file) == 'file')
        {
            $newfile = str_replace('[1]', '', $file);
            rename($dir . $file, $dir . $newfile);
        }
    }
    closedir($dh);
}

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template