PHP 删除文件与文件夹操作 unlink()与rmdir()这两个函数的使用_PHP教程

WBOY
Release: 2016-07-21 15:26:23
Original
1064 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;
}
}
?>

unlink() 函数用于删除文件。若成功,则返回 true,失败则返回 false。rmdir() 函数用于删除空的目录。它尝试删除 dir 所指定的目录。 该目录必须是空的,而且要有相应的权限。
一个实例:删除某个文件夹下的所有".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);
?>

www.bkjia.com true http://www.bkjia.com/PHPjc/323984.html TechArticle 先看一下代码 复制代码 代码如下: ? function deldir($dir) { //先删除目录下的文件: $dh=opendir($dir); while ($file=readdir($dh)) { if($file!="." $file!="..") {...
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
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!