Created
December 20, 2009 13:46
-
-
Save wtnabe/260488 to your computer and use it in GitHub Desktop.
`rm -r` command, PHP implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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