Home > Article > Backend Development > Modify file names in batches PHP method to modify file names in batches
The example in this article describes the method of batch modifying file names in PHP. Share it with everyone for your reference, the details are as follows:
<?php session_start(); set_time_limit(0); //function allfile($dir) // { // $files=array(); // if(is_file($dir)) // { // return $dir; // } // $handle = opendir($dir); // if($handle) { // while(false !== ($file = readdir($handle))) { // if ($file != '.' && $file != '..') { // $filename = $dir . "/" . $file; // if(is_file($filename)) { // $files[] = $filename; // }else { // // $files = array_merge($files, allfile($filename)); // } // } // } // end while // closedir($handle); // } // return $files; // // } // $dir="./*"; //print_r(allfile($dir)); $path = '.'; function get_filetree($path){ $tree = array(); foreach(glob($path.'/*') as $single){ if(is_dir($single)){ $tree = array_merge($tree,get_filetree($single)); } else{ $tree[] = $single; } } return $tree; } $dir_arr=get_filetree($path); $dir_arr_count=count($dir_arr); //echo $dir_arr_count."<br />"; for($i=0;$i<$dir_arr_count;$i++){ $file_name= $dir_arr[$i]; echo $file_name."<br />"; $file=basename($file_name); //echo $file."<br />"; $dir_arr_0_=explode($file,$file_name); $dir_arr_0_0=$dir_arr_0_[0]; $file_arr=explode("[1]",$file); ////rename $file_new_name=$file_arr[0].$file_arr[1]; rename($file_name,$dir_arr_0_0.$file_new_name); } ?>
Readers who are interested in more PHP related content can check out the special topics of this site: "php file operation summary", "php regular expression usage summary", "php operation Summary of office document skills (including word, excel, access, ppt)", "Complete of PHP array (Array) operation skills", "Summary of PHP sorting algorithm", "Summary of PHP common traversal algorithms and techniques", "PHP data structure and algorithm Tutorial", "Summary of PHP Programming Algorithms", "Summary of PHP Mathematical Operation Skills", "Summary of PHP Operations and Operator Usage", "Summary of PHP String Usage" and "Summary of PHP Common Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.
The above introduces the method of batch modifying file names in PHP, including batch modification of file names. I hope it will be helpful to friends who are interested in PHP tutorials.