上述代码是遍历当前目录所有文件,如何找出当前目录下某个后缀名的所有文件


 
<?php
 $path= dirname(__FILE__);//当前文件所在目录
  function file_list($path)  
  {  
      if ($handle = opendir($path))//打开路径成功   
      {  
          while (false !== ($file = readdir($handle)))//循环读取目录中的文件名并赋值给$file   
          {  
              if ($file != "." && $file != "..")//排除当前路径和前一路径   
             {  
                 if (is_dir($path."/".$file))  
                 {  
 //                    echo $path.": ".$file."<br>";//去掉此行显示的是所有的非目录文件   
                     file_list($path."/".$file);  
                 }  
                 else  
                 {  
                     echo $file." <a href=$file target=‘_blank’>下载</a> <br>";  
                     
                 }  
             }  
         }  
     }  
 }  
 echo  file_list($path);
 ?>

上述代码是遍历当前目录所有文件,如何找出当前目录下某个后缀名的所有文件?求大家给个思路或者案例!感谢!

<?php
function file_list($path, $extension)  
{  
    if ($handle = opendir($path))  
    {  
        while (false !== ($file = readdir($handle)))  
        {  
            if ($file != "." && $file != "..")  
            {  
                if (is_dir($path."/".$file))  
                {  
                    file_list($path."/".$file, $extension);  
                }  
                else  
                {  
                    $file_extension = pathinfo($file, PATHINFO_EXTENSION);
                    if ($file_extension == $extension) {
                        echo $file." <a href=$file target=‘_blank’>下载</a> <br>";  
                    }
                }  
            }  
        }  
    }  
}  
echo  file_list($path, "txt");
?>