Created
July 26, 2012 03:51
-
-
Save cisolarix/3180137 to your computer and use it in GitHub Desktop.
recursively deleting directory with sub-directories and files using SPL in PHP
This file contains hidden or 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
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