Skip to content

Instantly share code, notes, and snippets.

@davejlong
Created October 28, 2010 13:27
Show Gist options
  • Save davejlong/651341 to your computer and use it in GitHub Desktop.
Save davejlong/651341 to your computer and use it in GitHub Desktop.
A ColdFusion Function that Renders a File Size in an easily readable format (uses 1024)
<cffunction name="renderFileSize" access="public" output="false" hint="Returns file size in either b, kb, mb, gb, or tb">
<cfargument name="size" type="numeric" required="true" hint="File size to be rendered" />
<cfargument name="type" type="string" required="true" default="bytes" />
<cfscript>
local.newsize = ARGUMENTS.size;
local.filetype = ARGUMENTS.type;
do{
local.newsize = (local.newsize / 1024);
if(local.filetype IS 'bytes')local.filetype = 'KB';
else if(local.filetype IS 'KB')local.filetype = 'MB';
else if(local.filetype IS 'MB')local.filetype = 'GB';
else if(local.filetype IS 'GB')local.filetype = 'TB';
}while((local.newsize GT 1024) AND (local.filetype IS NOT 'TB'));
local.filesize = REMatchNoCase('[(0-9)]{0,}(\.[(0-9)]{0,2})',local.newsize);
if(arrayLen(local.filesize))return local.filesize[1] & ' ' & local.filetype;
else return local.newsize & ' ' & local.filetype;
return local;
</cfscript>
</cffunction>
@ianosullivan
Copy link

Perfect. Thanks Dave!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment