Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 11:55:18
Original
1024 people have browsed it

复制代码 代码如下:



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”文件夹(包括其内容也要被删除).

复制代码 代码如下:


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);
?>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!