<?php//PHP遍历文件夹下所有文件
$handle=opendir('.');  //这里输入其它路径
echo "目录 handle: $handle\n";
echo "文件:\n";
while ($file = readdir($handle)) {
    $a = explode('.',$file);
    if($a[1] == 'html' || $a[1] == 'hmt')
   {
    unlink($file);    
}
}
closedir($handle); 
?>

解决方案 »

  1.   

    遇到all.html这个名字的文件夹怎么办呢?
      

  2.   

    会楼上的
    用is_dir
    返回值bool
      

  3.   

    我自己搜了一个
    function tree($directory) 

    $mydir=dir($directory); 
    echo "<ul>\n"; 
    while($file=$mydir->read()){ 
    if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")) 
    {echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n"; 
    tree("$directory/$file"); 

    else{
        $a = explode('.',$file);
        if($a[1] == 'html' || $a[1] == 'htm')
       {
         //unlink($file);
    echo "<li>$file</li>\n"; 
    }

    }
    echo "</ul>\n"; 
    $mydir->close(); 

    //开始运行echo "<h2>目录为粉红色</h2><br>\n"; 
    tree("./");