note 36210 added to function.ini-get
| From: | myankele at not_cold_mail dot com | Date: | Wed, 01 Oct 2003 07:44:07 +0000 |
| Subject: | note 36210 added to function.ini-get | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-57519@lists.php.net to get a copy of this message | ||
Just as the manual says, you want to perform arithmetic and comparisons on "bytes". But
you also want to display Kilo and Mega bytes to the user.
These may be used to convert to and from Kilobytes and Megabytes notation.
<?php
// Convert numbers from G,M,K format into bytes
// - first argument is input string in 23.4M or 1.11K format
// - returned in an integer number of bytes
// Note: The returned result is truncated; make
// sure that it is what you want.
function convert_from_gmk($in_var) {
$var_len = strlen($in_var);
switch ($in_var{$var_len-1}) {
case 'K':
$mult = 1024;
break;
case 'M':
$mult = 1048576;
break;
case 'G':
$mult = 1073741824;
break;
default:
$mult = 1;
break;
}
return floor($in_var * $mult);
}
// Convert number of bytes into G,M,K format
// - first argument is a number,
// - second argument is the number of decimal
// places to round the result.
// - returned is the given number in a 23.4M or 100.22G form
function convert_into_gmk($in_var,$round = 1) {
if ($in_var < 1024) {
return $in_var;
} else if ($in_var < 1048576) {
$ret_val = round($in_var/1024,$round);
$ret_val = "$ret_val".'K';
} else if ($in_var < 1073741824) {
$ret_val = round($in_var/1048576,$round);
$ret_val = "$ret_val".'M';
} else {
$ret_val = round($in_var/1073741824,$round);
$ret_val = "$ret_val".'G';
}
return $ret_val;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.ini-get.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36210
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36210&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36210&report=yes