PHP Directory Delete Class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.