CFLib.org – Common Function Library Project

fncFileSize(number)

Last updated February 03, 2009

author

Kyle Morgan

Version: 3 | Requires: CF5 | Library: FileSysLib

Description:
After using a variety of different methods to generate file sizes of files being uploaded to a server for public downloads, frustration lead to writing of this tag. It will calculate in bytes, kilobytes, and megabytes, and return an accurate number to help plan/keep track of the size of files. To use the function, just use #fncFileSize(Number)# and it will return an accurate calculation. If you provide a variable that is not numerical, it will return 'Error'.

Return Values:
Returns a string.

Example:

<cfoutput>
#fncFileSize(120)#<br>
#fncFileSize(1675)#<br>
#fncFileSize(2082456)#<Br>
#fncFileSize(3791576)#<Br>
#fncFileSize(2813791576)#<Br>
</cfoutput>

Parameters:

Name Description Required
number Size in bytes of the file. Yes

Full UDF Source:

/**
 * Will take a number returned from a File.Filesize, calculate the number in terms of Bytes/Kilobytes/Megabytes and return the result.
 * v2 by Haikal Saadh
 * v3 by Michael Smith, cleaned up and added Gigabytes
 * 
 * @param number      Size in bytes of the file. (Required)
 * @return Returns a string. 
 * @author Kyle Morgan (admin@kylemorgan.com) 
 * @version 3, February 3, 2009 
 */
function fncFileSize(size) {
    if (size lt 1024) return "#size# b";
    if (size lt 1024^2) return "#round(size / 1024)# Kb";
    if (size lt 1024^3) return "#decimalFormat(size/1024^2)# Mb";
    return "#decimalFormat(size/1024^3)# Gb";
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson