Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created December 20, 2009 13:46
Show Gist options
  • Save wtnabe/260488 to your computer and use it in GitHub Desktop.
Save wtnabe/260488 to your computer and use it in GitHub Desktop.
`rm -r` command, PHP implementation
<?php
/**
* `rm -r` command, PHP implementation
*
* @since 2009-12-20
* @license two-clause BSD
*/
function rm_r( $path ) {
if ( is_dir( $path ) and $dh = opendir( $path ) ) {
while ( ($entry = readdir( $dh )) !== false ) {
if ( !preg_match( '/\A\.\.?\z/', $entry ) ) {
$p = $path.DIRECTORY_SEPARATOR.$entry;
if ( is_dir( $p ) ) {
rm_r( $p );
} else {
unlink( $p );
}
}
}
closedir( $dh );
rmdir( $path );
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment