diff options
author | Andreas Gohr <andi@splitbrain.org> | 2014-01-19 11:05:28 -0800 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2014-01-19 11:05:28 -0800 |
commit | 5838057a4fe97e039cc70bf30da3619cb9072c74 (patch) | |
tree | 10cf98cca3552f88d3209ebd60088ce916fd10f1 /inc | |
parent | ca605ff6763b98b580278988791dc5e57fc05a74 (diff) | |
parent | d8cf4dd43ecd37c371acf9bc6c17998b65d42ba4 (diff) | |
download | rpg-5838057a4fe97e039cc70bf30da3619cb9072c74.tar.gz rpg-5838057a4fe97e039cc70bf30da3619cb9072c74.tar.bz2 |
Merge pull request #494 from splitbrain/recursivedelete
Recursive delete function
Diffstat (limited to 'inc')
-rw-r--r-- | inc/io.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/inc/io.php b/inc/io.php index eff0279ac..892f93759 100644 --- a/inc/io.php +++ b/inc/io.php @@ -401,6 +401,56 @@ 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(!file_exists($path)) return true; // it's already gone or was never there, count as success + + 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 |