diff options
author | Andreas Gohr <andi@splitbrain.org> | 2014-01-06 20:20:15 +0100 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2014-01-06 20:49:42 +0100 |
commit | 4d47e8e3bcbb31435593de9d567723e5d87641bd (patch) | |
tree | 7910a86f19f4c787877a0c83fed889777296aa98 | |
parent | b7b43c1d69c59007b40925205e4e24962b3b6278 (diff) | |
download | rpg-4d47e8e3bcbb31435593de9d567723e5d87641bd.tar.gz rpg-4d47e8e3bcbb31435593de9d567723e5d87641bd.tar.bz2 |
added recursive delete function to io.php
-rw-r--r-- | inc/io.php | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/inc/io.php b/inc/io.php index eff0279ac..30f34ad3c 100644 --- a/inc/io.php +++ b/inc/io.php @@ -401,6 +401,55 @@ function io_mkdir_p($target){ } /** + * Recursively delete a directory + * + * @author Andreas Gohr <andi@splitbrain.org> + * @param string $path + * @param bool $removefiles defaults to false which will delete empty directories only + * @return bool + */ +function io_rmdir($path, $removefiles = false) { + if(!is_string($path) || $path == "") return false; + + if(is_dir($path) && !is_link($path)) { + $dirs = array(); + $files = array(); + + if(!$dh = @opendir($path)) return false; + while($f = readdir($dh)) { + if($f == '..' || $f == '.') continue; + + // collect dirs and files first + if(is_dir("$path/$f") && !is_link("$path/$f")) { + $dirs[] = "$path/$f"; + } else if($removefiles) { + $files[] = "$path/$f"; + } else { + return false; // abort when non empty + } + + } + closedir($dh); + + // now traverse into directories first + foreach($dirs as $dir) { + if(!io_rmdir($dir, $removefiles)) return false; // abort on any error + } + + // now delete files + foreach($files as $file) { + if(!@unlink($file)) return false; //abort on any error + } + + // remove self + return @rmdir($path); + } else if($removefiles) { + return @unlink($path); + } + return false; +} + +/** * Creates a directory using FTP * * This is used when the safemode workaround is enabled |