Skip to content

Instantly share code, notes, and snippets.

@drajathasan
Forked from jmwebservices/rrmdir.php
Created June 19, 2021 08:46
Show Gist options
  • Save drajathasan/4f8efd710fc4dcd6eb3ab31a8d1a97f5 to your computer and use it in GitHub Desktop.
Save drajathasan/4f8efd710fc4dcd6eb3ab31a8d1a97f5 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 );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment