Skip to content

Instantly share code, notes, and snippets.

@cisolarix
Created July 26, 2012 03:51
Show Gist options
  • Save cisolarix/3180137 to your computer and use it in GitHub Desktop.
Save cisolarix/3180137 to your computer and use it in GitHub Desktop.
recursively deleting directory with sub-directories and files using SPL in PHP
function recursiveDelDirecoty( $dir, $delMe = FALSE ) {
$dir = realpath( $dir );
try{
$it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST );
foreach( $it as $i ) {
if ( $i->isDir() ) {
rmdir( $i->getRealPath() );
echo "删除". $i . "目录成功";
}else{
unlink( $i->getRealPath() );
echo "删除". $i . "文件成功";
}
}
}catch( Exception $e ) {
echo $e->getMessage();
}
if ( $delMe == TRUE ) {
rmdir( $dir );
echo "删除". $dir . "目录成功";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment