Need a method to eliminate a directory along with all its files and nested directories in PHP.
PHP provides a comprehensive solution for this task, allowing you to delete a directory and all its contents recursively. Here's a user-contributed implementation from the rmdir manual page:
function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (is_dir($dir. DIRECTORY_SEPARATOR . $object) && !is_link($dir . "/" . $object)) { rrmdir($dir . DIRECTORY_SEPARATOR . $object); } else { unlink($dir . DIRECTORY_SEPARATOR . $object); } } } rmdir($dir); } }
To use this function, simply provide the path to the directory you want to delete:
rrmdir('path/to/directory');
The above is the detailed content of How to Recursively Delete a Directory and Its Contents in PHP?. For more information, please follow other related articles on the PHP Chinese website!