加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php删除文件夹及其文件夹下所有文件的函数代码

发布时间:2020-05-24 16:51:40 所属栏目:PHP 来源:互联网
导读:有时候我们需要用php删除文件夹及其文件夹下所有文件,那么就可以使用下面的代码了,需要的朋友可以参考下。根据自身要求添加功能

<div class="codetitle"><a style="CURSOR: pointer" data="39156" class="copybut" id="copybut39156" onclick="doCopy('code39156')"> 代码如下:<div class="codebody" id="code39156">
<?
function deldir($dir) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
?>

实例:删除某个文件夹下的所有“.svn”文件夹(包括其内容也要被删除).
<div class="codetitle"><a style="CURSOR: pointer" data="30870" class="copybut" id="copybut30870" onclick="doCopy('code30870')"> 代码如下:<div class="codebody" id="code30870">
<?php
function delsvn($dir) {
$dh=opendir($dir);
//找出所有".svn“ 的文件夹:
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(is_dir($fullpath)) {
if($file==".svn"){
delsvndir($fullpath);
}else{
delsvn($fullpath);
}
}
}
}
closedir($dh);
}
function delsvndir($svndir){
//先删除目录下的文件:
$dh=opendir($svndir);
while($file=readdir($dh)){
if($file!="."&&$file!=".."){
$fullpath=$svndir."/".$file;
if(is_dir($fullpath)){
delsvndir($fullpath);
}else{
unlink($fullpath);
}
}
}
closedir($dh);
//删除目录文件夹
if(rmdir($svndir)){
return true;
}else{
return false;
}
} $dir=dirname(FILE);
//echo $dir;
delsvn($dir);
?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读