Home > Backend Development > PHP Tutorial > PHP batch modification file name program_PHP tutorial

PHP batch modification file name program_PHP tutorial

WBOY
Release: 2016-07-13 16:55:26
Original
726 people have browsed it

Provide two php codes that use traversal to modify the file names of files in batches. Friends in need can refer to them.

Example 1

 代码如下 复制代码
//利用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');
?>

Example 2

The code is as follows
 代码如下 复制代码

$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);
}

Copy code
$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);
}

http://www.bkjia.com/PHPjc/631685.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/631685.html
TechArticle provides two php codes that use traversal to modify the file names of files in batches. Friends in need can refer to it. Example 1 The code is as follows Copy the code ?php //Use PHP directory and file functions to traverse...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template