Home  >  Article  >  Backend Development  >  How to traverse the folder and modify the file name in the folder in php

How to traverse the folder and modify the file name in the folder in php

藏色散人
藏色散人Original
2021-06-10 09:37:271929browse

How to traverse a folder and modify the name of the file in the folder: first read the folder; then traverse the folder through the "foreach($temp as $v){...}" method; finally pass The "@rename($a,$new_name);" method can be used to modify the file name.

How to traverse the folder and modify the file name in the folder in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php traverse the folder and modify the files under the folder? Name?

php traverses the folder file name and changes the file name

<?php
 
 
 
function list_file($date){
        //1、首先先读取文件夹
        $temp=scandir($date);
        //遍历文件夹
        foreach($temp as $v){
            $a=$date.&#39;/&#39;.$v;
           if(is_dir($a)){//如果是文件夹则执行
          
               if($v==&#39;.&#39; || $v==&#39;..&#39;){//判断是否为系统隐藏的文件.和..  如果是则跳过否则就继续往下走,防止无限循环再这里。
                   continue;
               }
               echo "<font color=&#39;red&#39;>$a</font>","<br/>"; //把文件夹红名输出
             
               list_file($a);//因为是文件夹所以再次调用自己这个函数,把这个文件夹下的文件遍历出来
           }else{
            echo $a."<br/>";
$info = pathinfo($a);
//$file_name =  basename($a,&#39;.&#39;.$info[&#39;extension&#39;]);
$kuozhan=$info["extension"];
$lujing=$info["dirname"];
$mingcheng=$info["filename"];
if($mingcheng!="4" and $mingcheng!="5"){
$mingcheng_new=base64_encode($mingcheng);
$new_name=$lujing."/".$mingcheng_new.".".$kuozhan;
@rename($a,$new_name);
}
 //var_dump($info); //echo "*****".$file_name;
           }
          
        }
    }
 
list_file(&#39;F:/MYOA&#39;);
 
?>

[Recommended learning: PHP video tutorial]

The above is the detailed content of How to traverse the folder and modify the file name in the folder 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