Today we introduce a PHP directory delete class, this PHP directory delete class is very convenient to use, regardless of whether the given directory is empty, he can be all and its subdirectories are deleted.
Let's take a look at the details of this PHP directory removal class:
Start with a new PHP file named removeclass.inc.php. Of course, the name can be based on your preferences, as long as the naming rules and convenient reference. The code is as follows:
<?php
##############################################################
# Class RemoveDir
#
# Class Design Purpose:
# Delete a directory, regardless of whether the directory is empty
#
#
#
Method
# * REMOVEDIR ()-Constructors
# * ISEMPTY ($path)-Determine if the directory is empty
# * DELETEDIR ([$dirnm])-Delete directory and subdirectories
##############################################################
Class RemoveDir
{
Private $dirnm;
function RemoveDir () {}//constructor
function IsEmpty ($path)//Determine if the directory is empty
{
$handle = Opendir ($path);
$i = 0;
while (false!== ($file = Readdir ($handle)))
$i + +;
Closedir ($handle);
if ($i >= 2)
return false;
Else
return true;
}
function Deletedir ($DIRNM)//delete contents of directory and subdirectories
{
$d = Dir ($dirnm);
while (false!== ($entry = $d->read ()))
{
if ($entry = = '. ' | | $entry = = ' ... ')
Continue
$currele = $d->path. ' /'. $entry;
if (Is_dir ($currele))
{
if ($this->isempty ($currele))
@rmdir ($currele);
Else
$this->deletedir ($currele);
}
Else
@unlink ($currele);
}
$d->close ();
RmDir ($DIRNM);
return true;
}
}
?>
This PHP directory removal class is very simple to use, for example, create a new PHP file:
Include ("removeclass.inc.php");//Remove the PHP directory from the class reference
$obj =new RemoveDir (); Instantiating a PHP directory delete class
$obj->deletedir (". /testing/"); Call the Delete method to delete the testing folder under the parent directory
Class is very simple, I believe you can easily understand, in the course of your work, if you encounter the use of PHP to delete the directory, this class is a good choice, of course, if you understand the principle of this class implementation, write yourself a recursive delete function also
PHP Directory Delete Class