note 61036 added to function.delete

From: Date: Tue, 24 Jan 2006 06:09:46 +0000
Subject: note 61036 added to function.delete
Groups: php.notes 
Request: Send a blank email to php-notes+get-102331@lists.php.net to get a copy of this message
Try this for a real delete() function instead. I made it so it unsets the variable instead of setting it to an empty string: /** * string delete(mixed vars, mixed type) * * 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: * 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. */ 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)) { $vars = array($vars); // Make it an array to reduce the ammount of code required. } 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 = ''; while (list($k, $v) = each($vars)) { // Actual type may override supplied type if (($type[$k] == IS_DIR || strtolower($type[$k]) == 'dir') || is_dir($v)) { rmdir($v); if (!is_dir($v)) { $report .= "Successfully removed directory, '$v'.<br />\\n"; } else { $report .= "Failed to remove directory, '$v'.<br />\\n"; } } else if (($type[$k] == IS_FILE || strtolower($type[$k]) == 'file') || is_file($v)) { unlink($v); if (!is_file($v)) { $report .= "Successfully deleted file, '$v'.<br />\\n"; } else { $report .= "Failed to delete file, '$v'.<br />\\n"; } } else if (($type[$k] == IS_VAR || strtolower($type[$k]) == 'var') || isset($v)) { global $$v; // Get the actual memory space (in case 'REGISTER_GLOBALS' is off). $$v = ''; if ($$v == '') { $report .= "Successfully unset variable, '" . '$' . "$v'.<br />\\n"; } else { $report .= "Failed to unset variable, '" . '$' . "$v'.<br />\\n"; } } else { $report .= "$v is not valid.<br />\\n"; // Should never see this, but you never know. } } 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+61036 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61036&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61036&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61036&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61036&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61036&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61036&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61036&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61036&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102331) next »