这是以前做的一个类似dos下的deltree命令的函数,用的就是递归。可以参考一下。

解决方案 »

  1.   

    <?
    function deltree($dir){
    chdir($dir);
    $handle=opendir('.');
    while (($file=readdir($handle))<>"") {
    if(is_file($file))
    unlink($file);
    if(is_dir($file) && $file<>"." && $file<>"..")
    deltree($file);
    }
    closedir($handle); 
    }
    deltree('test');
    ?>
      

  2.   

    while ($filename = readdir($handle)) 
    =>while (($filename = readdir($handle) !==false) 
    Get($newpath,&$a);
    =>Get($newpath,$a);
    既然是在windows环境中,就将/变为\好了。
      

  3.   

    网上有很多这样的代码呵,
    我也不知是谁写的了
    <?
    $FileCount = 0; //文件个数
    $DirectoryCount = 0; //目录个数
    function FCount($Path)
    {
    global $FileCount,$DirectoryCount;
        $Handle = opendir($Path);
        while($File = readdir($Handle))
        {
            if(filetype($Path.$File) != 'dir')
            {
                echo "----文件名----$File<br>";
                $FileCount++;
            }
            if($File !='.' && $File !='..' && filetype($Path.$File)=='dir')
            {
                echo "目录名----$File<br>";
                $DirectoryCount++;
                FCount($Path."$File/");
            }
        }
        closedir($Handle);
    }FCount('./hyforum_code/');
    echo "<br>请您注意脚本程序30秒钟超时警告<br><br>目录个数……………………$DirectoryCount<br>----文件个数……………………$FileCount<br>";
    ?>
      

  4.   

    我写的一个删除程序, 没发现有什么遇目录名为0删不掉的问题.function delfile($path)
    {
      echo "<dl>";
      echo "<dt>删除目录 <font color=green>".$path." </font>……<br>";  $handle=@opendir($path);
      @chdir($path);  while (($file=@readdir($handle))!="")
      {
        if (($file==".") OR ($file=="..")) continue;    if (is_dir($file))
          delfile($file);
        else
        {
          echo "<dd>删除文件 <font color=green>".$file."</font> …… ";
          if (@unlink($file))
            echo "<font color=blue>成功!</font><br>";
          else
            echo "<font color=red>失败!</font><br>";
        }
      }  @closedir($handle);   @chdir("..");  echo "<dt>删除目录 <font color=green>".$path."</font> …… ";
      if (@rmdir($path))
        echo "<font color=blue>成功!</font><br>";
      else
        echo "<font color=red>失败!</font><br>";
      
      echo "</dl>";
    }
    ?>
      

  5.   

    遇到目录名为0的会删不掉: 这是因为类似于while($File = readdir($Handle))的判断, 会把"0"转成0, 结果为假. uGain的和我的就不会.但是uGain的程序不能正确删除子目录, 因为if(is_dir($file) && $file<>"." && $file<>"..")这个判断有问题, PHP中好像没有<>...是!=才对吧. 不知道为什么PHP竟然不报错. 呵呵~
      

  6.   

    楼上的,做个简单的测试吧。
    <?
    if(1<>2) echo "hahahahaha";
    ?>
      

  7.   


    while ($filename = readdir($handle))
    改为
    while (($filename = readdir($handle))!="")
      

  8.   

    uGain, 我查了手册了, sorry, <>和!=是一样的, 我没注意:)
    不过你的程序的确是不行:P
      

  9.   

    呵呵,是啊,当时没有考虑目录名为“0”的情况。
    好象在linux下,0是无效目录名吧。
      

  10.   

    uGain, 你的程序不是"0"的问题, 一般的目录都删除不掉......
      

  11.   

    while ($filename = readdir($handle))
    =>while ($filename = readdir(strval($handle)))