使用php修改指定文件后缀

WBOY
Release: 2016-06-23 13:50:01
Original
1118 people have browsed it

最近要将asp后缀的修改成php,因懒于一个个修改。又觉得php跟Qt一样都是高级语言了,一般高级语言都有提供对获得的内容进行增删改查的函数。于是乎,就上网搜了下,还真不少,故些将所用心得总结下来。

目标:将当前目录下的asp后缀改成php,而不影响其它“后缀格式的文件”,而且只是针对“当前文件夹”,对当前文件夹内包含的文件夹的文件不进行修改。

  代码如下:

<?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');?>
Copy after login

另外:附上获取文件扩展名的四种方法: http://www.jb51.net/article/29765.htm

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!