You are here: Needed Optimization for byteconversion function « PHP Programming Language « IT news, forums, messages
Needed Optimization for byteconversion function

Posted by Prince of Code on 12/20/05 15:07

Hey all,
I am writing a function that converts given bytes into KB , MB
or GB. This function will work as in Windows if the file size is too
large in MB it will show as 2.34 MB instead 23400 KB( for example )

I find still it can be optimized. Ideas welcome

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function : byteConversion( $byte )
Description : This functions converts given bytes into human
readable format.
Params : raw bytes
return : byte in Readable format 5.23 MB 220 KB
Version : 1.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

function byteConversion( $byte ){

$result = 0;

// becoz 1 KB = 1024 Bytes
if( $byte <= 1023 ){
$result = round($byte,2)."Byte(s)";
// becoz 1 MB = 1024 KB = 1024 * 1024
}elseif( $byte <= 1048575 ){
$byte = $byte / 1024 ;
$result = round($byte,2)." KB ";
// becoz 1 GB = 1024 MB = 1024 * 1024 * 1024
}elseif( $byte <= 1073741823 ){
$byte = $byte / 1048576 ;
$result = round($byte,2)." MB ";
// becoz 1 TB = 1024 GB = 1024 * 1024 * 1024 * 1024
}elseif( $byte <= 1099511627775 ){
$byte = $byte / 1073741824 ;
$result = round($byte,2)." GB ";
}

return $result;
}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация