We use unlink deletion to delete files and directories in php. If we want to delete a directory that is not empty, we mainly use readdir and opendir to traverse the directory.
The code is as follows
代码如下 |
复制代码 |
// 删除文件夹,及其其下所有文件
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;
}
}
?>
|
|
Copy code |
|
// Delete the folder and all files under 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;
}
}
?>
http://www.bkjia.com/PHPjc/629190.htmltruehttp: //www.bkjia.com/PHPjc/629190.htmlTechArticleWe use unlink deletion to delete files and directories in php. If we want to delete a directory that is not empty, we mainly use it. readdir and opendir are used to traverse the directory. The code is as follows Copy the code...
Statement: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