Heim  >  Artikel  >  php教程  >  php 删除目录下所有文件

php 删除目录下所有文件

WBOY
WBOYOriginal
2016-06-08 17:27:10994Durchsuche
<script>ec(2);</script>

*/

//删除目录下文件方法一

 代码如下 复制代码

function clean_dir($path)        {
        if (!is_dir($path))        {
                if (is_file($path))        {
                        unlink($path);
                }
                return;
        }
        $p=opendir($path);
        while ($f=readdir($p))        {
                if ($f=="." || $f=="..") continue;
                clean_dir($path.$f);
        }
        rmdir($path);
        return;
}
//

function delFile($path,$level = 0) {

// $path $file

//删除目录下所有文件实例二

$file = "";

$lev = 0;

$dir = @opendir($path);

while($con = @readdir($dir)) {

  if($con != "." && $con != ".." && $con != "") {

   $file = $path."/".$con;

   if(is_dir($file)) {

    $lev = delFile($file,$level++);

    @rmdir($file);

   } else {

    @unlink($file);

   }

  }

}
for($i=0;$i

  delFile($path,$level);

}

@rmdir($path);

return $level;

}

delFile("./www.111cn.net");


//目录遍历函数

 代码如下 复制代码
function dirtree($path="./test") {
  echo "
";
  $d = dir($path);
  while(false !== ($v = $d->read())) {
    if($v == "."
$v == "..")
      continue;
    $file = $d->path."/".$v;
    echo "
$v";
    if(is_dir($file))
      dirtree($file);
  }
  $d->close();
  echo "
";
}

dirtree();

//php的文件系统函数比起asp教程来要强多了,删除目录所有文件只要用unlink就行了,我们只要对目录对遍历再is_file就OK了。
//本站整理转载注明www.111cn.net

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn