Created
October 28, 2010 13:27
-
-
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)
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
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perfect. Thanks Dave!