Plugins:FileUtils

<< Back to Dashboard

Contents

Exploring the FileUtils Plugin

Overview

This is a File Utilities CFC

createFile

Create a new empty file using java.io.File.

Returns

  • This function returns void

Arguments

Key Type Required Default Description
filename String Yes --- The absolute path of the file to create.

Examples

getPlugin("FileUtils").createFile(expandPath('./MyNewFile.text'));

appendFile

Facade to append to a file's content

Returns

  • This function returns void

Arguments

Key Type Required Default Description
FileToSave any Yes --- The absolute path to the file.
FileContents any Yes --- The file contents
CharSet string No utf-8 CF File CharSet Encoding to use.
CheckCharSetFlag boolean No false Check the charset.

Examples

getPlugin("FileUtils").appendFile(expandPath('./MyNewFile.text'),"New Content");

getFileMimeType

Get's the file mime type for a given file extension

Returns

  • This function returns string

Arguments

Key Type Required Default Description
extension string Yes --- e.g. jpg or gif

Examples

getPlugin("FileUtils").getFileMimeType("jpg");
// produces: image/jpg
getPlugin("FileUtils").getFileMimeType("ppt");
// produces: application/vnd.ms-powerpoint

isFile

Checks whether the filename argument is a file or not.

Returns

  • This function returns boolean

Arguments

Key Type Required Default Description
Filename String Yes --- The absolute path of the file to check.

Examples

if( NOT getPlugin("FileUtils").isFile(expandPath('MyFile.txt')) ){
  // it is a directory, recurse it
}

fileLastModified

Get the last modified date of a file

Returns

  • This function returns string

Arguments

Key Type Required Default Description
filename string Yes ---

Examples

if( getSetting('LoadTime') lt fileLastModified(expandPath('config/Coldbox.cfc')) ){
  // new file, reload app
  controller.setColdboxInitiated(false);
}

getAbsolutePath

Turn any system path, either relative or absolute, into a fully qualified one

Returns

  • This function returns string

Arguments

Key Type Required Default Description
path string Yes --- Abstract pathname

Examples

absPath = getPlugin("FileUtils").getAbsolutePath("/coldbox/config/settings.xml");

fileSize

Get the filesize of a file.

Returns

  • This function returns string

Arguments

Key Type Required Default Description
filename string Yes ---
sizeFormat string No bytes Available formats: [bytes][kbytes][mbytes][gbytes]

Examples

// file size in bytes
#getPlugin("FileUtils").fileSize(expandPath('myfile.zip'))#

// file size in kbytes
#getPlugin("FileUtils").fileSize(expandPath('myfile.zip'),"kbytes")#

// file size in mbytes
#getPlugin("FileUtils").fileSize(expandPath('myfile.zip'),"mbytes")#

fileCanWrite

Check wether you can write to a file

Returns

  • This function returns boolean

Arguments

Key Type Required Default Description
Filename String Yes --- The absolute path of the file to check.

Examples

if( getPlugin("FileUtils").fileCanWrite(expandPath('myLog.log') ){
  getPlugin("FileUtils").appendFile(expandPath('myLog.log'),"My new log entry");
}

readFile

Facade to Read a file's content

Returns

  • This function returns Any

Arguments

Key Type Required Default Description
FileToRead String Yes --- The absolute path to the file.
ReadInBinaryFlag boolean No false Read in binary flag.
CharSet string No CF File CharSet Encoding to use.
CheckCharSetFlag boolean No false Check the charset.

Examples

var contents = getPlugin("FileUtils").readFile( expandPath('myLog.log') );

// Read binary
var image = getPlugin("FileUtils").readFile( expandPath('myImage.png'), true );

isDirectory

Check wether the filename argument is a directory or not

Returns

  • This function returns boolean

Arguments

Key Type Required Default Description
Filename String Yes --- The absolute path of the file to check.

Examples

if( getPlugin("FileUtils").isDirectory(expandPath('config')) ){
  // recurse the directory now
}

uploadFile

Facade to upload to a file, returns the cffile variable.

Returns

  • This function returns any

Arguments

Key Type Required Default Description
FileField string Yes --- The name of the form field used to select the file
Destination string Yes --- The absolute path to the destination.
NameConflict string No makeunique Action to take if filename is the same as that of a file in the directory.
Accept string No Limits the MIME types to accept. Comma-delimited list.
Attributes string No Normal Comma-delimitted list of window file attributes
Mode string No 755 The mode of the file for Unix systems, the default is 755

Examples

// Upload a file
getPlugin("FileUtils").uploadFile(fileField="fileImage",destination=expandPath('images'));

// Upload a file
getPlugin("FileUtils").uploadFile(fileField="fileImage",destination=expandPath('images'),accept="image/jpg,image/png");

sendFile

Send a file to the browser

Returns

  • This function returns void

Arguments

Key Type Required Default Description
file any No The absolute path to the file or a binary file
name string No The name to send the file to the browser. If not sent in, it will use the name of the file or a UUID for a binary file
mimeType string No A valid mime type to use. If not sent in, we will try to use a default one according to file extension
disposition string No attachment The browser content disposition (attachment/inline)
abortAtEnd boolean No false Do an abort after content sending
extension string No Only used if file is binary. e.g. jpg or gif
deleteFile string No false Delete the file after sending. Only used if file is not binary

Examples

// deliver a file via cfcontent
getPlugin("FileUtils").sendFile(file=expandPath('images/myDynamicImage.png'),deleteFile=true);

// deliver a pdf
getPlugin("FileUtils").sendFile(file=expandPath('images/myDynamicImage.png'),name="MyStatement.pdf",disposition="attachment");

//deliver binary
binaryData = service.getDynamicData();
getPlugin("FileUtils").sendFile(file=binaryData,name="content.png",extension="png");

fileCanRead

Check wether you can read a file

Returns

  • This function returns boolean

Arguments

Key Type Required Default Description
Filename String Yes --- The absolute path of the file to check.

Examples

if( getPlugin("FileUtils").fileCanRead( expandPath('config/log.log') ) ){
  contents = getPlugin("FileUtils").readFile( expandPath('config/log.log') );
}

removeFile

Remove a file using java.io.File

Returns

  • This function returns boolean

Arguments

Key Type Required Default Description
filename string Yes --- The absolute path to the file.

Examples

deleted = getPlugin("FileUtils").removeFile( expandPath('config/log.log') );

ripExtension

Rip the extension of a filename.

Returns

  • This function returns string

Arguments

Key Type Required Default Description
filename string Yes ---

Examples

extension = getPlugin("FileUtils").ripExtension( 'config/log.log' );

directoryCopy

Copies an entire source directory to a destination directory

Returns

  • This function returns void

Arguments

Key Type Required Default Description
source string Yes ---
destination string Yes ---
nameconflict any Yes overwrite

Examples

getPlugin("FileUtils").copyDirectory( expandPath('updates/newRelease'), expandPath('repo/core'), true );

saveFile

Facade to save a file's content

Returns

  • This function returns void

Arguments

Key Type Required Default Description
FileToSave any Yes --- The absolute path to the file.
FileContents any Yes --- The file contents
CharSet string No utf-8 CF File CharSet Encoding to use.
CheckCharSetFlag boolean No false Check the charset.

Examples

getPlugin("FileUtils").saveFile( expandPath('logs/mynewFile.txt'), "My new file contents" );

 
Download in other Formats:
markup Markup | pdf PDF | html HTML | word Word

comments Comments (0)


ColdBox Book

book