Home >Backend Development >PHP Tutorial >Delete php program in non-empty directory_PHP tutorial

Delete php program in non-empty directory_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:55:381131browse

First check if there is a file in this directory. If there is, if it is a folder, then call this function to delete it. If it is a file, directly call unlink to delete it, and finally delete the directory.

The code is as follows
 代码如下 复制代码

function removeDir($dirName)
{
if(! is_dir($dirName))
{
return false;
}
$handle = @opendir($dirName);
while(($file = @readdir($handle)) !== false)
{
if($file != '.' && $file != '..')
{
$dir = $dirName . '/' . $file;
is_dir($dir) ? removeDir($dir) : @unlink($dir);
}
}
closedir($handle);

return rmdir($dirName) ;
}
?>

Copy code
function removeDir($dirName)
{
If(! is_dir($dirName))
{
         return false;
}  
$handle = @opendir($dirName);
While(($file = @readdir($handle)) !== false)
{
If($file != '.' && $file != '..')
                                                                                               $dir = $dirName . '/' . $file;
                 is_dir($dir) ? removeDir($dir) : @unlink($dir);
                                            }  
closedir($handle);

Return rmdir($dirName);
}
?>

http://www.bkjia.com/PHPjc/631650.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631650.html
TechArticle
First check whether there is a file in this directory. If there is, if it is a folder, then call this function to delete it. If If it is a file, directly call unlink to delete it, and finally delete this directory. The code is as follows...
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