note 58830 deleted from function.rmdir by aidan
| From: | aidan@php.net | Date: | Wed, 15 Apr 2009 04:28:26 +0000 |
| Subject: | note 58830 deleted from function.rmdir by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153270@lists.php.net to get a copy of this message | ||
Note Submitter: andy at mentalist dot co dot uk
----
# Clears temporary directory and any files or sub directories
# using a stack, without evil recursion.
#
# Andy Gale - 17/11/2005
function clear_tmp_dir($dir)
{
$stack = array($dir);
while (count($stack)) {
# Get last directory on stack
$dir = end($stack);
$dh = opendir($dir);
if (!$dh) {
trigger_error('clear_tmp_dir: unable to opendir ' . $dir, E_USER_ERROR);
}
while (($file = readdir($dh)) !== false) {
if ($file == '.' or $file == '..') {
continue;
}
if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
$stack[] = $dir . DIRECTORY_SEPARATOR . $file;
} else if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
unlink($dir . DIRECTORY_SEPARATOR . $file);
} else {
trigger_error('clear_tmp_dir: ignoring ' . $dir .
DIRECTORY_SEPARATOR . $file, E_USER_ERROR);
}
}
if (end($stack) == $dir) {
rmdir($dir);
array_pop($stack);
}
}
}