As we all know, the PHP rmdir () function is used to delete empty directories, but if you want to delete a non-empty directory, we must delete the files or subdirectories in the non-empty directory, this article introduces how PHP uses recursive functions to delete non-empty directories, the need for friends can refer to.
First, let me introduce you to 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 results in a e_warning level error.
Parameters:
DirName: The path to the directory.
Context: Added support for contexts in PHP 5.0.0.
PHP rmdir () Delete a non-empty directory
As mentioned above, the RmDir () function can only delete empty directories, if the non-empty directories need to go to the directory first, use the unlink () function to delete each file in the directory, and then come back to delete the empty directory. If subdirectories exist in the directory, and the directory is not empty, you need to use recursive methods. The source code for the custom recursive function delete directory is as follows:
We can also use the operating system command "RM-RF" to remove non-empty directories, but also from security and cross-platform considerations, as far as possible, to handle recursive deletion of non-empty directories.