First, let's introduce the rmdir () function.
PHP rmdir () function
rmdir-Delete Empty Directory
Grammar:
BOOL RmDir (String $dirname [, Resource $context])
An attempt was made to delete the directory specified by DirName. The directory must be empty and have the appropriate permissions. Failure can result in a e_warning level error.
Parameters:
1.dirname: The path to the directory.
2.context: Added support for context in PHP 5.0.0.
PHP rmdir () Delete non-empty directory
As mentioned above, the RmDir () function can only delete empty directories, if the Non-empty directory needs to enter the directory first, use the unlink () function to delete every file in the directory, and then come back to delete this empty directory. If subdirectories also exist in the directory, and the directory is not empty, you need to use a recursive method. Custom recursive function deletes the source code of the directory as follows:
<?php function
Deldir ($directory) {//Custom function recursive functions the entire directory if
(file_exists ($directory)) {//Determine whether the directory exists, if no rmdir () function can error
if ($dir _handle= @opendir ($directory)) {//Open Directory return directory resource and determine if success while
($filename =readdir ($dir _handle) {//Traverse directory, read out files or folders in directory
if ($filename!= '. ' && $filename!= '. ') {//must exclude two special directory
$subFile = $directory. " /". $filename;//Connect the files in the directory to the current directory if
(Is_dir ($subFile)) {//Deldir ($subFile) If it is a directory condition
;//recursively call yourself to delete the subdirectory
}
if (Is_file ($subFile)) {//If a file condition is set up
unlink ($subFile);//delete this file directly}}
closedir ($ Dir_handle);//Close Directory Resource
rmdir ($directory);//Delete Empty Directory}}}
deldir ("Mydir");//Call Deldir function
?>
Handling recursive deletion of non-empty directories, we can also use the operating system command "RM-RF" to remove Non-empty directories, but also from the security and cross-platform considerations as far as possible not to use.
Thank you for reading, I hope to help you, thank you for your support for this site!