The example of this article summarizes two methods of PHP recursive deletion of the specified folder. Share to everyone for your reference. Specifically as follows:
Method One:
function Recursivedelete ($dir)
{
if ($handle = @opendir ($dir))
{while
($file = Readdir ($handle))!== False)
{
if ($file = = ".") | | ($file = = ".."
) {
continue;
}
if (Is_dir ($dir. '/' . $file))
{
//call self to this directory
Recursivedelete ($dir. '/' . $file);
}
else
{
unlink ($dir. '/' . $file); Remove this file
}
}
@closedir ($handle);
RmDir ($dir);
}
Method Two:
/*
Custom delete function, can delete file and recursive delete folder
* * Function
My_del ($path)
{
if (Is_dir ($path))
{
$file _ list= Scandir ($path);
foreach ($file _list as $file)
{
if ($file!= '. ' && $file!= ' ... ')
{
My_del ($path. ') /'. $file);
}
@rmdir ($path);
This method does not need to determine whether the folder is empty,//Because whether the folder is empty at the beginning or not, it
is empty
}
else
{
@unlink ($path);
These two places are best to use @ Block warning error, look annoyed
}
}
$path = ' d:/Technical document-copy ';
folder to delete
//If PHP file is not ANSI, but UTF-8 mode,
//and the folder to be deleted contains kanji characters, the call to the function requires a transcoding
//$path =iconv (' Utf-8 ', ' gb2312 ', $path);
My_del ($path);
I hope this article will help you with your PHP program design.