Use of the unlink () and rmdir () functions for deleting files and folders in PHP. Let's take a look at the code copy code as follows :? Functiondeldir ($ dir) {delete the files in the directory first: $ dhopendir ($ dir); while ($ filereaddir ($ dh) {if ($ file !. $ File !..) {Check the code first
The 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;
}
}
?>
The unlink () function is used to delete objects. If the call succeeds, true is returned. if the call fails, false is returned. The rmdir () function is used to delete an empty directory. It tries to delete the directory specified by the dir. The directory must be empty and have the corresponding permissions.
One instance: delete all the ". svn" folders under a folder (including the content to be deleted ).
The code is as follows:
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 );
?>
The pipeline code is as follows :? Function deldir ($ dir) {// delete the files in the directory first: $ dh = opendir ($ dir); while ($ file = readdir ($ dh )) {if ($ file! = "." $ File! = ".."){...