diff options
author | Anika Henke <anika@selfthinker.org> | 2014-02-02 14:43:06 +0000 |
---|---|---|
committer | Anika Henke <anika@selfthinker.org> | 2014-02-02 14:43:06 +0000 |
commit | a051c2744a397f5ccbb688e0edab307451dc522d (patch) | |
tree | d9744944e50f2c23df22bd6856779bd228e129c4 /inc/io.php | |
parent | 3641199a253e8f92f378f03926af80724ef04146 (diff) | |
parent | a8895ef5de970d5e6b1e273c3fe111b606611244 (diff) | |
download | rpg-a051c2744a397f5ccbb688e0edab307451dc522d.tar.gz rpg-a051c2744a397f5ccbb688e0edab307451dc522d.tar.bz2 |
Merge remote-tracking branch 'origin/master' into video-audio
Diffstat (limited to 'inc/io.php')
-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..c5225a2e0 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(false !== ($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 |