note 61052 deleted from function.delete by nlopess
| From: | nlopess@php.net | Date: | Tue, 24 Jan 2006 15:22:05 +0000 |
| Subject: | note 61052 deleted from function.delete by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-102370@lists.php.net to get a copy of this message | ||
Note Submitter: egingell AT sisna DOT com
----
Sure would be nice if we could edit our own notes. But I digress. Here's a much better
refinement.
/**
* string delete(mixed vars, mixed type)
*
* This function will:
* rmdir($v) if $v is a directory.
* unlink($v) if $v is a file.
* unset($GLOBALS[$v]) if $v is a variable name.
* or fail if $v doesn't resolve to anything.
*
* Returns status report or false if no valid parameter is supplied
* or array($type) and array($vars) don't have the same number of elements
*
* $vars and $type must have the same element count or both be strings.
*
* Valid values in $type:
* 1, 2, 3, IS_DIR, IS_FILE, IS_VAR, 'dir', 'file', and 'var'.
*
* Note:
* Do not include '$' in var name.
* Var names, whether supplied in an array or not, must be strings.
* In the case of this funtion, a string is a single element array.
* If any supplied name(s) resolve to more than one type, all will be deleted.
* For example: if var '$foo' is set and file 'foo' exist, both will be
deleted.
* Non unique params will result in "'$v' doesn't exist."
* For example (assume that var '$foo' is set and file 'foo' exists):
* This function call:
echo delete(
array(
'foo',
'foo'
)
);
* Will output (source view):
Array<br />
(<br />
[0] => foo<br />
[1] => foo<br />
)<br />
Successfully deleted file, 'foo'.<br />
Successfully emptied variable, '$foo'.<br />
'foo' doesn't exist.<br />
*
**/
define(IS_DIR, 1);
define(IS_FILE, 2);
define(IS_VAR, 3);
function delete($vars = null, $type = null) {
if (is_null($vars)) {
trigger_error("You need to supply at least 1 file, directory, or variable."); // I would
rather this give a warning than die
return false;
}
if (!is_array($vars)) {
// Make it an array to reduce the amount of code required.
// This works because PHP is a loose-typed language.
$vars = array($vars);
}
if (!is_null($type)) {
if (!is_array($type)) {
$type= array($type); // Make it an array to reduce the ammount of code required.
}
if (count($type) != count($vars)) {
trigger_error('$type, if supplied, must have the same number of elements as $vars.');
return false;
}
// We don't really care if the arrays are key or index based, so change all the keys to
0-based indexes.
$temp = array();
foreach ($type as $v) {
$temp[] = $v;
}
$type = $temp;
}
// We don't really care if the arrays are key or index based, so change all the keys to
0-based indexes.
$temp = array();
foreach ($vars as $v) {
$temp[] = $v;
}
$vars = $temp;
$report = nl2br(print_r($vars, true));
if (!is_array($type)) $report .= nl2br(print_r($type, true));
while (list($k, $v) = each($vars)) {
// Actual type may override supplied type
$somethingDone = false;
if (($type[$k] == IS_DIR || strtolower($type[$k]) == 'dir') || is_dir(@realpath($v))) {
$somethingDone = true;
@rmdir(@realpath($v));
if (!is_dir(@realpath($v))) {
$report .= "Successfully removed directory, '$v'.<br />\n";
} else {
$report .= "Failed to remove directory, '$v'.<br />\n";
}
}
if (($type[$k] == IS_FILE || strtolower($type[$k]) == 'file') || is_file(@realpath($v)))
{
$somethingDone = true;
@unlink(@realpath($v));
if (!is_file(@realpath($v))) {
$report .= "Successfully deleted file, '$v'.<br />\n";
} else {
$report .= "Failed to delete file, '$v'.<br />\n";
}
}
if (($type[$k] == IS_VAR || strtolower($type[$k]) == 'var') || isset($GLOBALS[$v])) {
$somethingDone = true;
if (isset($GLOBALS[$v])) {
unset($GLOBALS[$v]);
if (!isset($GLOBALS[$v])) {
$report .= "Successfully emptied variable, '" . '$' .
"$v'.<br />\n";
} else {
$report .= "Failed to empty variable, '" . '$' . "$v'.<br
/>\n";
}
}
}
unset($vars[$k]); // Remove $v from the $vars array.
if (!$somethingDone) {
$report .= "'$v' doesn't exist.<br />\n";
}
}
return $report;
}