note 61052 added to function.delete
| From: | egingellATsisnaDOTcom at osu1 dot php dot net | Date: | Tue, 24 Jan 2006 14:56:02 +0000 |
| Subject: | note 61052 added to function.delete | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102361@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 216.194.113.175
Probable Submitter: 65.100.200.115
----
Manual Page -- http://www.php.net/manual/en/function.delete.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61052
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61052&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61052&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61052&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61052&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61052&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61052&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61052&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61052&report=yes
Search -- http://master.php.net/manage/user-notes.php