Home  >  Article  >  Backend Development  >  PHP custom function to recursively delete files and directories_PHP tutorial

PHP custom function to recursively delete files and directories_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:35:03688browse

Copy code The code is as follows:

/*—————————————————— * /
//– Recursively delete files and directories
//– Example: del_dir ('../cache/'); Note: the returned / is required
//– $type forces directory deletion , true yes, false no
/*———————————————————— */
function del_dir ($dir,$type=true)
{
$n=0;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) ! == false) {
//.svn ignore svn version control information
if ( $file == '.' or $file =='..' or $file == '.svn')
{
continue;
}
if (is_file ($dir.$file))
{
unlink($dir.$file);
$n++;
}
if (is_dir ($dir.$file))
{
del_dir ($dir.$file.'/');
if ($type)
{
$n++;
rmdir($dir.$file.'/');
}
}
}
}
closedir($dh);
}
return $n;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322314.htmlTechArticleCopy the code as follows: /*———————————————— — */ //– Recursively delete files and directories //– Example: del_dir ('../cache/'); Note: The returned / must be...
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