Knowledgebase

UDT: bytesToSize

August 21, 2013

Create a new UDT, and call it 'bytesToSize'.

Paste the following code into the new UDT.

$units = array('B', 'KB', 'MB', 'GB', 'TB');

$bites = max($params['bytes'], 0);

$pow = floor(($bites ? log($bites) : 0) / log(1024));
$pow = min($pow, count($units) - 1);

$bites /= pow(1024, $pow);

echo (round($bites, 1) . ' ' . $units[$pow]);

To use it, just place the following in your template, where $x is the var that contains the number/value you wish to convert:

{bytesToSize bytes=$x}
Tags: UDT,conversions,filesize