Page 1 of 1

byte conversion

Posted: Tue Mar 25, 2003 4:26 pm
by Zoram
Is there a function that i missed when i looked or doesn someone know a function that would accept a number (bytes) and convert it to the biggest unit that would be above 0?

For example :

Code: Select all

function convertByte( $bytes ) {
	$sizes = array( " Bytes", " KB", " MB", " GB" );
	$size = 0;
	while ( $bytes >= 1024 ) {
		$bytes /= 1024;
		$size++;
	}
	return number_format($bytes) . $sizesї$size];
}
Would that be right?

Posted: Wed Mar 26, 2003 5:13 am
by lazy_yogi

Code: Select all

function format_size($rawSize) {
     if	($rawSize / 1048576 > 1)
           return round($rawSize/1048576, 1) . 'MB';
     else if	($rawSize / 1024 > 1)	
           return round($rawSize/1024, 1) . 'KB';
     else
           return round($rawSize, 1) . 'bytes';
  }