Home  >  Article  >  Backend Development  >  How to correctly use PHP recursive functions to delete non-empty directories and subdirectories?

How to correctly use PHP recursive functions to delete non-empty directories and subdirectories?

WBOY
WBOYOriginal
2016-07-25 08:51:25842browse
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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn