Created
July 26, 2012 07:11
-
-
Save cisolarix-snippets/3180696 to your computer and use it in GitHub Desktop.
PHP: recursively delete directory with files and/or sub-directories in it
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(); | |
} | |
unset( $it ); | |
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