Skip to content

Instantly share code, notes, and snippets.

@jmwebservices
Forked from liconti/rrmdir.php
Last active November 3, 2022 20:49
Show Gist options
  • Save jmwebservices/986d9b975eb4deafcb5e2415665f8877 to your computer and use it in GitHub Desktop.
Save jmwebservices/986d9b975eb4deafcb5e2415665f8877 to your computer and use it in GitHub Desktop.
PHP recursive rmdir
<?php
/**
* Recursively empty and delete a directory
*
* @param string $path
* @ref https://gist.github.com/jmwebservices/986d9b975eb4deafcb5e2415665f8877
*/
function rrmdir( string $path ) : void
{
if( trim( pathinfo( $path, PATHINFO_BASENAME ), '.' ) === '' )
return;
if( is_dir( $path ) )
{
array_map( 'rrmdir', glob( $path . DIRECTORY_SEPARATOR . '{,.}*', GLOB_BRACE | GLOB_NOSORT ) );
@rmdir( $path );
}
else
@unlink( $path );
}
?>
@jmwebservices
Copy link
Author

Forked from https://gist.github.com/liconti/2426567 and modified to recognize (and delete) files containing two or more leading dots.

@drajathasan
Copy link

Nice. it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment