php recursively delete the directory (folder)

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags .net code delete delete directory deleted directory echo examples

recursive delete directory (folder) Here are two examples, one is to delete a separate empty directory code, a batch delete directory folder code.

// delete a single empty folder

$ dir = 'www.111cn.net';
if (is_dir ($ dir)) / / to determine whether the directory
{
if (rmdir ($ dir))
{
Echo 'directory deleted successfully';
}
else
{
echo 'did not delete the directory permissions';
}
}
else
{
echo 'is not a valid directory';
}
Let's look at a batch recursive delete function of the directory.

function deletedir ($ dir) {
if (! handle = @ opendir ($ dir)) {// Check if the directory to open exists
die ("no such directory");
}
while (false! == ($ file = readdir ($ handle))) {
if ($ file! == "." && $ file! == "..") {// exclude the current directory from the parent directory
$ file = $ dir .directory_separator. $ file;
if (is_dir ($ file)) {
deletedir ($ file);
} else {
if (@unlink ($ file)) {
echo "file <b> $ file </ b> deleted successfully."
} else {
echo "file <b> $ file </ b> failed to delete! <br>";
}
}
}
if (@rmdir ($ dir)) {
echo "directory <b> $ dir </ b> deleted successfully. <br> n";
} else {
echo "directory <b> $ dir </ b> failed to delete! <br> n";
}
}

//test program
$ dir = "/ var / www / test";
deletedir ($ dir);

Related Article

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.