Php function code used to delete all files in folders and their folders

Source: Internet
Author: User

Copy codeThe Code is as follows:
<?
Function deldir ($ dir ){
// Delete the files in the directory first:
$ Dh = opendir ($ dir );
While ($ file = readdir ($ dh )){
If ($ file! = "." & $ File! = ".."){
$ Fullpath = $ dir. "/". $ file;
If (! Is_dir ($ fullpath )){
Unlink ($ fullpath );
} Else {
Deldir ($ fullpath );
}
}
}
Closedir ($ dh );
// Delete the current folder:
If (rmdir ($ dir )){
Return true;
} Else {
Return false;
}
}
?>

Instance: delete all the ". svn" folders under a folder (including the content to be deleted ).
Copy codeThe Code is as follows:
<? Php
Function delsvn ($ dir ){
$ Dh = opendir ($ dir );
// Find all ". svn" Folders:
While ($ file = readdir ($ dh )){
If ($ file! = "." & $ File! = ".."){
$ Fullpath = $ dir. "/". $ file;
If (is_dir ($ fullpath )){
If ($ file = ". svn "){
Delsvndir ($ fullpath );
} Else {
Delsvn ($ fullpath );
}
}
}
}
Closedir ($ dh );
}
Function delsvndir ($ svndir ){
// Delete the files in the directory first:
$ Dh = opendir ($ svndir );
While ($ file = readdir ($ dh )){
If ($ file! = "." & $ File! = ".."){
$ Fullpath = $ svndir. "/". $ file;
If (is_dir ($ fullpath )){
Delsvndir ($ fullpath );
} Else {
Unlink ($ fullpath );
}
}
}
Closedir ($ dh );
// Delete the directory folder
If (rmdir ($ svndir )){
Return true;
} Else {
Return false;
}
}

$ Dir = dirname (_ FILE __);
// Echo $ dir;
Delsvn ($ dir );
?>

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.