CFLib.org – Common Function Library Project

fncFileSize(number)

Last updated February 3, 2009

Version: 3 | Requires: ColdFusion 5 | Library: FileSysLib

 
Rated 6 time(s). Average Rating: 3.2

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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";
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
16 day(s) ago

James Sleeman James Sleeman added
quickSort
26 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
29 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson