note 36210 deleted from function.ini-get by sniper
| From: | sniper@php.net | Date: | Fri, 24 Oct 2003 12:24:12 +0000 |
| Subject: | note 36210 deleted from function.ini-get by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-59217@lists.php.net to get a copy of this message | ||
Note Submitter: myankele@not_cold_mail.com
----
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;
}
?>