Home  >  Article  >  Backend Development  >  Use php to modify the specified file suffix_PHP tutorial

Use php to modify the specified file suffix_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:18:58782browse

Use php to modify the specified file suffix

Recently I have to change the asp suffix to php, because I am too lazy to change 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. So, I searched online and found quite a lot, so I summarized my experience.

Goal: Change the asp suffix in the current directory to php without affecting other "files in suffix format", and only for the "current folder", without modifying the files in the folders contained in the current folder. .

The 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!=&#39;..&#39;)
	{ 
	  if(is_dir($dirname.$file))
		{ 
			echo $dirname.$file.<br/-->; 
			//foreachDir($dirname.$file);  //如果注释号去掉,将会递归修改文件夹内的文件夹文件
		}
	  else
		{ 
			echo --.$dirname./.$file.
; 
			 $temp = substr($file, strrpos($file, &#39;.&#39;)+1);  //获取后缀格式
		    if ($temp == asp)	
			   {
				$pos = strripos($file,&#39;.&#39;);  //获取到文件名的位置
				$filename = substr($file,0,$pos);  //获取文件名
				rename($dirname.&#39;/&#39;.$file,$dirname.&#39;/&#39;.$filename.&#39;.php&#39;); //替换为php后缀格式。
			  }

		} 
	} 
 } 

} 
	 
foreachDir(&#39;../traverseMendFilename&#39;);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/879188.htmlTechArticleUsing php to modify the specified file suffix. Recently, I have 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 ​​​​are provided to obtain...
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