php修改指定文件后缀的方法_php技巧

WBOY
Release: 2016-05-16 20:36:16
Original
1182 people have browsed it

因项目需求,需要将asp后缀的修改成php,因懒于一个个修改。又觉得php跟Qt一样都是高级语言了,一般高级语言都有提供对获得的内容进行增删改查的函数。经过一番资料查找与代码测试,总结出了PHP修改指定文件后缀的方法,分享给大家。

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

具体功能代码如下:

<&#63;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');
&#63;>

Copy after login

感兴趣的朋友可以测试运行并扩展完善本文实例,相信会对大家PHP程序设计的学习有所帮助。

另外:附上获取文件扩展名的四种方法供大家参考。

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!