首页 > 后端开发 > php教程 > PHP删除非空目录的三种方法(代码分享)_PHP教程

PHP删除非空目录的三种方法(代码分享)_PHP教程

WBOY
发布: 2016-07-20 11:15:08
原创
865 人浏览过


有了这个小程序,PHPer就不用手动进行删除计算机上的目录文件了,在练习PHP目录文件操作时就可以用这个函数,在这个基础上还可以增加浏览文件夹目录,然后进行删除。

代码一:

<?<span php
 </span><span //</span><span 删除非空目录</span>
<span function</span> d_rmdir(<span $dirname</span><span ) {
</span><span if</span>(!<span is_dir</span>(<span $dirname</span><span )) {
</span><span return</span> <span false</span><span ;
}
</span><span $handle</span> = @<span opendir</span>(<span $dirname</span><span );
</span><span while</span>((<span $file</span> = @<span readdir</span>(<span $handle</span>)) !== <span false</span><span ){
</span><span if</span>(<span $file</span> != '.' && <span $file</span> != '..'<span ){
</span><span $dir</span> = <span $dirname</span> . '/' . <span $file</span><span ;
</span><span is_dir</span>(<span $dir</span>) ? d_rmdir(<span $dir</span>) : <span unlink</span>(<span $dir</span><span );
}
}
</span><span closedir</span>(<span $handle</span><span );
</span><span return</span> <span rmdir</span>(<span $dirname</span><span ) ;
}
</span><span if</span>(d_rmdir("./temp"<span ))
</span><span echo</span> "succes"<span ;
</span><span else</span>
<span echo</span> "false"<span ;
</span><span //</span><span by www.jbxue.com</span>
?>
登录后复制

代码二,来自php中文在线手册:

<?<span php
</span><span //</span><span 删除非空目录 
//by www.jbxue.com</span>
functionremove_directory(<span $dir</span><span ){
 </span><span if</span>(<span $handle</span>=<span opendir</span>("<span $dir</span>"<span )){
 </span><span while</span>(<span false</span>!==(<span $item</span>=<span readdir</span>(<span $handle</span><span ))){
  </span><span if</span>(<span $item</span>!="."&&<span $item</span>!=".."<span ){
   </span><span if</span>(<span is_dir</span>("<span $dir</span>/<span $item</span>"<span )){
    remove_directory(</span>"<span $dir</span>/<span $item</span>"<span );
   }</span><span else</span><span {
    </span><span unlink</span>("<span $dir</span>/<span $item</span>"<span );
    </span><span echo</span>"removing<span $dir</span>/<span $item</span><br> "<span ;
   }
  }
 }
 </span><span closedir</span>(<span $handle</span><span );
 </span><span rmdir</span>(<span $dir</span><span );
 </span><span echo</span>"removing<span $dir</span><br> "<span ;
 }
}
</span>?>
登录后复制

代码三:

<?<span php
</span><span //</span><span 删除非空目录
//by www.jbxue.com</span>
functionremoveDir(<span $dirName</span><span )
{
  </span><span if</span>(!<span is_dir</span>(<span $dirName</span><span ))
  {
    returnfalse;
  }
  </span><span $handle</span>=@<span opendir</span>(<span $dirName</span><span );
  </span><span while</span>((<span $file</span>=@<span readdir</span>(<span $handle</span>))!==<span false</span><span )
  {
    </span><span if</span>(<span $file</span>!='.'&&<span $file</span>!='..'<span )
    {
      </span><span $dir</span>=<span $dirName</span>.'/'.<span $file</span><span ;
      </span><span is_dir</span>(<span $dir</span>)?removeDir(<span $dir</span>):@<span unlink</span>(<span $dir</span><span );
    }
  }
  </span><span closedir</span>(<span $handle</span><span );  
  returnrmdir(</span><span $dirName</span><span );
}
</span>?>
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440229.htmlTechArticle有了这个小程序,PHPer就不用手动进行删除计算机上的目录文件了,在练习PHP目录文件操作时就可以用这个函数,在这个基础上还可以增加...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板