Created
April 24, 2012 11:11
-
-
Save faffyman/2478828 to your computer and use it in GitHub Desktop.
Smarty Modifier: readable_filesize - displays a filesize in an easier to read format/ best units available.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Smarty plugin | |
* | |
* @package App | |
* @subpackage SmartyPluginsModifier | |
*/ | |
/** | |
* Smarty number modifier | |
* | |
* Type: modifier | |
* Name: readable_filesize | |
* Purpose: allows you to present a filesize in the best possible units | |
* Useage: {$nFilesize|readable_filesize} or by specifiying a unit {$nFilesize|readable_filesize:'MB'} | |
* | |
* @link | |
* @author Tim Swann <https://github.com/faffyman/> | |
* @param int $nFilesize | |
* @param string $sUnits preferred units specified 'KB','MB','GB' This will over-ride the default | |
* @param int $decimals number of decimal places to work to. | |
* @return string | |
*/ | |
function smarty_modifier_readable_filesize($nFilesize, $sUnits=null, $decimals = null) | |
{ | |
// if user specified bytes just return the same number input | |
if($sUnits=='KB') { | |
return number_format($nFilesize, $decimals) . ' KB'; | |
} | |
$aAllowedUnits = array('KB','MB','GB','TB','PB','EB','ZB','YB'); | |
if ( empty($sUnits) || !in_array($sUnits, $aAllowedUnits )) { | |
if ($nFilesize < 1000) { | |
return $nFilesize . ' KB'; | |
} else { | |
//$nFilesize = $nFilesize / 1024; | |
if ($nFilesize < 1024) { | |
$decimals = is_null($decimals) || !is_numeric($decimals) ? 0 : intval($decimals); | |
return number_format($nFilesize, $decimals) . ' KB'; | |
} else { | |
if ($nFilesize < pow(1024,2) ) { | |
$decimals = is_null($decimals) || !is_numeric($decimals) ? 2 : intval($decimals); | |
$nFilesize = $nFilesize / 1024; | |
return number_format($nFilesize, $decimals) . ' MB'; | |
} else { | |
$decimals = is_null($decimals) || !is_numeric($decimals) ? 2 : intval($decimals); | |
$nFilesize = $nFilesize / pow(1024,2); | |
return number_format($nFilesize, $decimals) . ' GB'; | |
} | |
} | |
} | |
} else { | |
switch ($sUnits) { | |
// DEFAULT IS KB | |
case 'KB': | |
$nDivideBy = 1; | |
break; | |
case 'MB': | |
$nDivideBy = 1024; | |
break; | |
case 'GB': | |
//$nDivideBy = 1024 * 1024 * 1024; | |
$nDivideBy = pow(1024,2); | |
break; | |
case 'TB': | |
//$nDivideBy = 1024 * 1024 * 1024 * 1024; | |
$nDivideBy = pow(1024,3); | |
break; | |
case 'PB': | |
//$nDivideBy = 1024 * 1024 * 1024 * 1024 * 1024; | |
$nDivideBy = pow(1024,4); | |
break; | |
case 'EB': | |
//$nDivideBy = 1024 * 1024 * 1024 * 1024 * 1024 * 1024; | |
$nDivideBy = pow(1024,5); | |
break; | |
case 'ZB': | |
//$nDivideBy = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024; | |
$nDivideBy = pow(1024,6); | |
break; | |
case 'YB': | |
//$nDivideBy = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024; | |
$nDivideBy = pow(1024,7); | |
break; | |
default: | |
$nDivideBy = 1; | |
break; | |
} | |
$decimals = is_null($decimals) || !is_numeric($decimals) ? 2 : intval($decimals); | |
$nFilesize = $nFilesize / $nDivideBy; | |
return number_format($nFilesize, $decimals) . $sUnits; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment