byte conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

byte conversion

Post 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?
Last edited by Zoram on Wed Mar 26, 2003 9:51 am, edited 1 time in total.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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';
  }
Post Reply