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.
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.'/'.$v; if(is_dir($a)){//如果是文件夹则执行 if($v=='.' || $v=='..'){//判断是否为系统隐藏的文件.和.. 如果是则跳过否则就继续往下走,防止无限循环再这里。 continue; } echo "<font color='red'>$a</font>","<br/>"; //把文件夹红名输出 list_file($a);//因为是文件夹所以再次调用自己这个函数,把这个文件夹下的文件遍历出来 }else{ echo $a."<br/>"; $info = pathinfo($a); //$file_name = basename($a,'.'.$info['extension']); $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('F:/MYOA'); ?>
[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!