note 125812 added to function.rmdir
| From: | anonymous_user@php.net | Date: | Fri, 12 Feb 2021 14:48:25 +0000 |
| Subject: | note 125812 added to function.rmdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219309@lists.php.net to get a copy of this message | ||
// Recursive PHP function to completely remove a directory
function delete_directory_recursively( $path ) {
$dir = new \DirectoryIterator( $path );
// Iterate through the subdirectories / files of the provided directory
foreach ( $dir as $dir_info ) {
// Exclude the . (current directory) and .. (parent directory) paths
// from the directory iteration
if ( ! $dir_info->isDot() ) {
// Get the full currently iterated path
$iterated_path = $dir_info->getPathname();
// If the currently iterated path is a directory
if ( $dir_info->isDir() ) {
// which is not empty (in which case scandir returns an array of not 2 (. and ..)
elements)
if ( count( scandir( $iterated_path ) ) !== 2 ) {
// Call the function recursively
self::remove_directory_recursively( $iterated_path );
} else {
// if the currently iterated path is an empty directory, remove it
rmdir( $iterated_path );
}
} elseif ( $dir_info->isFile() ) {
// If the currently iterated path describes a file, we need to
// delete that file
unlink( $iterated_path );
}
} // loop which opens if the currently iterated directory is neither . nor ..
} // end of iteration through directories / files of provided path
// After iterating through the subpaths of the provided path, remove the
// concerned path
rmdir( $path );
} // end of delete_directory_recursively() function definition
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 213.55.224.91)
----
Manual Page -- http://php.net/manual/en/function.rmdir.php
Edit -- https://master.php.net/note/edit/125812
Del: integrated -- https://master.php.net/note/delete/125812/integrated
Del: useless -- https://master.php.net/note/delete/125812/useless
Del: bad code -- https://master.php.net/note/delete/125812/bad+code
Del: spam -- https://master.php.net/note/delete/125812/spam
Del: non-english -- https://master.php.net/note/delete/125812/non-english
Del: in docs -- https://master.php.net/note/delete/125812/in+docs
Del: other reasons-- https://master.php.net/note/delete/125812
Reject -- https://master.php.net/note/reject/125812
Search -- https://master.php.net/manage/user-notes.php