Far too often professionals in the technological field use excessive verbage to make a seemingly straightforward topic far too complicated than is necessary. The same is often true, at least in this case, of their programming abilities. (This is where we insert the snide comment about my own abilities.) Forget it, it's not important. The ability to speak succinctly is a unique talent, no matter the language or medium. I've had English majors as instructors that tore my written papers apart, while at the same time their lectures were off-topic or rarely got the point across. Program code is amazing to me and although I am always working on my skills, I find solice in the least amount of input for the greatest output. Following I have three common routines that show a progression of programming capability, ranging from amateurish to professional.
Web people use functions like these all the time. The final output is typically identical though the implementations can differ greatly as this example illustrates. It is a bit of source code that I originally found in The PHP Anthology, a book I paid entirely too much for (actually didn't download, go me). The egos from these so-called "world-class developers", the authors, tout their egos up a bit with this text taken directly from the book:
Five world-class developers guide you through the remarkable capabilities of PHP using countless examples of best-practice programming.
Right, let's put it to the test, em? Since PHP's filesize() function returns nothing useful for humans, we use another routine to format the final result for some tangible application, namely "1.62kB", rather than "1661".
Check out this BEST PRACTICE PROGRAMMING routine!
function fileSizeUnit( $size )
{
if( $size >= 1073741824 ) {
$size=number_format( ( $size / 1073741824 ),2 );
$unit='GB';
} elseif( $size >= 1048576 ) {
$size=number_format( ( $size / 1048576 ),2 );
$unit='MB';
} elseif( $size >= 1024 ) {
$size=number_format( ( $size / 1024 ),2 );
$unit='KB';
} elseif( $size >= 0 ) {
$unit='B';
} else {
$size='0';
$unit='B';
}
return array( 'size'=>$size,'unit'=>$unit );
}It's a mess, returning an array that over-complicates my usage of its output. The sheer number of comparisons is ridiculous. Nearly every value is hard-coded and all over the place, making quick edits a (filtered) nightmare.
So because I needed a function like this back when I bought the book I resorted to the resources available in the PHP manual online. The user contributions can sometimes be spectacular. Here is a perfect reason of why this is so.
function get_size( $size )
{
$bytes=array( 'B','KB','MB','GB','TB' );
foreach( $bytes as $val ) {
if( $size > 1024 ) {
$size=$size / 1024;
} else {
break;
}
}
return round( $size,2 ).$val;
}This function begins to illustrate the only requirement for formatting the size passed to it: divide until less than 1,024. Nothing else to do but assign a string value from the handy array that is easily understood. I was very happy with this until I stumbled across this, which appeared some time later.
function bytestostring( $size,$precision = 0 )
{
$sizes=array( 'YB','ZB','EB','PB','TB','GB','MB','kB','B' );
$total=count( $sizes );
while( $total-- && $size > 1024 ) $size /= 1024;
return round( $size,$precision ).$sizes[$total];
}With this routine there is the added complexity of using precision for how defining close you wish to round the returned value, and yet it is miles ahead of other routines I've gone through. There are more but these three functions stood out the most to me. You can see here that the final result is identical except for the returned array from the Anthology freaks.
% ./filesizes.php fileSizeUnit: Array ([size] => 1.57 [unit] => KB) get_size: 1.32KB bytestostring: 1.32kB
I've been going back over things I wrote when beginning PHP and starting from scratch, so I appreciate the obsession and desire put into perfecting code. I don't take credit for any of these functions - the authors are acknowledge below where you can download these things and jack around with 'em.
filesizes.phps
#!/usr/bin/php
<?php
/*
% ./filesizes.php
fileSizeUnit: Array ([size] => 1.57 [unit] => KB)
get_size: 1.32KB
bytestostring: 1.32kB
*/
function fileSizeUnit( $size )
{
/*
PHP Anthology:
"Five world-class developers guide
you through the remarkable capabilities
of PHP using countless examples of best-
practice programming."
*/
if( $size >= 1073741824 ) {
$size=number_format( ( $size / 1073741824 ),2 );
$unit='GB';
} elseif( $size >= 1048576 ) {
$size=number_format( ( $size / 1048576 ),2 );
$unit='MB';
} elseif( $size >= 1024 ) {
$size=number_format( ( $size / 1024 ),2 );
$unit='KB';
} elseif( $size >= 0 ) {
$unit='B';
} else {
$size='0';
$unit='B';
}
return array( 'size'=>$size,'unit'=>$unit );
}
function get_size( $size )
{
/* marcel-hergerdt dot de */
/* http://us3.php.net/manual/en/function.filesize.php */
$bytes=array( 'B','KB','MB','GB','TB' );
foreach( $bytes as $val ) {
if( $size > 1024 ) {
$size=$size / 1024;
} else {
break;
}
}
return round( $size,2 ).$val;
}
function bytestostring( $size,$precision = 0 )
{
/* php-manual at gone dot nl */
/* http://us3.php.net/manual/en/function.filesize.php */
$sizes=array( 'YB','ZB','EB','PB','TB','GB','MB','kB','B' );
$total=count( $sizes );
while( $total-- && $size > 1024 ) $size /= 1024;
return round( $size,$precision ).$sizes[$total];
}
$f="./filesizes.php";
print "fileSizeUnit: " .fileSizeUnit( filesize( $f ) );
print "\nget_size: " .get_size( filesize ( $f ) );
print "\nbytestostring: ".bytestostring( filesize ( $f ),2 );
print "\n";
?>












