note 61035 deleted from function.delete by nlopess

From: Date: Tue, 24 Jan 2006 15:21:59 +0000
Subject: note 61035 deleted from function.delete by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-102368@lists.php.net to get a copy of this message
Note Submitter: egingell AT sisna DOT com ---- Try this for a real delete() function: /** * 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 emptied variable, '" . '$' . "$v'.<br />\\n"; } else { $report .= "Failed to empty variable, '" . '$' . "$v'.<br />\\n"; } } else { $report .= "$v is not valid.<br />\\n"; // Should never see this, but you never know. } } return $report; }

« previous php.notes (#102368) next »