How to modify the file suffix in php: first open the directory through the opendir function; then read the directory; then obtain the suffix format and the location of the file name; and finally modify the file suffix through the rename method.
The operating environment of this tutorial: windows10 system, php5.6. This article is applicable to all brands of computers.
Recommended: "PHP Video Tutorial"
Due to project requirements, it is necessary to change the asp suffix to php, because I am too lazy to modify it one by one. I also feel that php, like Qt, is a high-level language. Generally, high-level languages provide functions for adding, deleting, modifying and checking the obtained content.
Goal: Change the asp suffix in the current directory to php without affecting other "files in suffix format", and only for the "current folder", files in the folders contained in the current folder No modifications are made.
The specific function code is as follows:
<?php function foreachDir($dirname) { if(!is_dir($dirname)) { echo "{$dirname} not effective dir"; exit(); } $handle=opendir($dirname); //打开目录 while (($file = readdir($handle))!==false) //读取目录 { if($file!="." && $file!='..') { if(is_dir($dirname.$file)) { echo $dirname.$file."<br/>"; //foreachDir($dirname.$file); //如果注释号去掉,将会递归修改文件夹内的文件夹文件 } else { echo "--".$dirname."/".$file."<br/>"; $temp = substr($file, strrpos($file, '.')+1); //获取后缀格式 if ($temp == "asp") { $pos = strripos($file,'.'); //获取到文件名的位置 $filename = substr($file,0,$pos); //获取文件名 rename($dirname.'/'.$file,$dirname.'/'.$filename.'.php'); //替换为php后缀格式。 } } } } } foreachDir('../traverseMendFilename'); ?>
The above is the detailed content of How to modify file suffix in php. For more information, please follow other related articles on the PHP Chinese website!