Home > Article > Backend Development > How to correctly use PHP recursive functions to delete non-empty directories and subdirectories?
How to use PHP recursive functions to correctly delete non-empty directories, how to delete PHP directories and files, when encountering non-empty directories, it is a good choice to consider using PHP recursion to implement directory traversal and deletion.
php recursive function The idea of recursively deleting directories: 1. PHP recursively traverses directories and subdirectories 2. Use scandir to determine whether the directory is empty. If it is empty, use rmdir to delete it. Example, php recursive function to delete all empty directories: Copy code Code example:Using shell is much simpler: find target folder -mindepth 1 -depth -empty -type d -exec rm -r {} ;2. PHP method to delete empty directories (non-php recursive function method) How to delete empty directories in php, tips on using rmdir function php deletes an empty directory through the rmdir() function Example: Copy code Code example:The first execution will return: Directory removed. The second execution, because the directory has been deleted, will return: Directory does not exist. |