You want to remove a directory and all of its contents, including subdirectories and their contents.
Use RecursiveDirectoryIterator and RecursiveIteratorIterator, specifying that children (files and subdirectories) should be listed before their parents, as in Example 24-26.
Obliterating a directory
Removing files, obviously, can be dangerous. Because PHP's built-in directory removal function, rmdir( ), works only on empty directories, and unlink( ) can't accept shell wildcards, the RecursiveIteratorIterator must be told to provide children before parents with its CHILD_FIRST constant.
However, that constant is not available before PHP 5.1. If you're using an earlier version of PHP, you can use the function in Example 24-27 for the same purpose.
Obliterating a directory without RecursiveIteratorIterator
24.11.4. See Also
Documentation on rmdir( ) at http://www.php.net/rmdir and on RecursiveIteratorIterator at http://www.php.net/~helly/php/ext/spl/classRecursiveIteratorIterator.html.