cffile
Manages interactions with server files. Different combinations cause different attributes to be required.
<cffile action="read">
fileRead(path);
Attribute Reference
action string Required
appendcopydeletemovereadreadbinaryrenameuploaduploadallwrite
file string
mode string
output string
addnewline boolean
Default: true
true
truefalse
attributes string
readonlyhiddennormalsystemtemporary
charset string
utf-8iso-8859-1windows-1252us-asciishift_jisiso-2022-jpeuc-jpeuc-krbig5euc-cnutf-16
source string
destination string
variable string
filefield string
nameconflict string
errorskipoverwritemakeunique
accept string
result variableName
Default: cffile
cffile
fixnewline string
Default: false
false
truefalse
cachedwithin numeric
Compatibility
BoxLang:
<bx:file> in BoxLang Links more information about cffile
- Tips for Secure File Uploads with CFML - Blog entry by Pete Freitag outlining some tips to improve security when dealing with file uploads in CFML.
Examples Sample code using the cffile tag
File Write
fileWrite(expandPath("./myFile.txt"), "Here's some content for my file."); File Append - There is no fileAppend() so we access the file and use fileWriteLine()
myFile = fileOpen(expandPath("./myFile.txt"), "append");
fileWriteLine(myFile, "Here's some new content.");
fileClose(myFile); File Read
myFile = fileRead(expandPath("./myFile.txt")); File Read Binary
myImageBinary = fileReadBinary(expandPath("./myImage.jpg")); File Rename - Since there is no fileRename(), fileMove() works just as well
fileMove(expandPath("./myFile.txt"), expandPath("./myNewFileName.txt")); File Copy
fileCopy(expandPath("./myFile.txt"), expandPath("./some/other/path")); File Move
fileMove(expandPath("./myFile.txt"), expandPath("./some/other/path")); File Delete
fileDelete(expandPath("./myFile.txt")); File Upload
fileUpload(getTempDirectory(), "form.myFile", " ", "makeunique"); File Upload All
fileUploadAll(getTempDirectory(), " ", "makeunique"); Write the contents of a variable to a file.
<cffile action="write" file="#expandPath("./myFile.txt")#" output="Here's some content for my file."> Append content to the end of a file.
<cffile action="append" file="#expandPath("./myFile.txt")#" attributes="normal" output="Here's some new content."> Read a file into a variable
<cffile action="read" file="#expandPath("./myFile.txt")#" variable="myFile"> File Read Binary
<cffile action="readBinary" file="#expandPath("./myImage.jpg")#" variable="myImageBinary"> Rename a file
<cffile action="rename" source="#expandPath("./myFile.txt")#" destination="#expandPath("./myNewFileName.txt")#" attributes="normal"> Copy a file
<cffile action="copy" source="#expandPath("./myFile.txt")#" destination="#expandPath("./some/other/path")#"> Move a file
<cffile action="move" source="#expandPath("./myFile.txt")#" destination="#expandPath("./some/other/path")#"> Delete a file
<cffile action="delete" file="#expandPath("./myFile.txt")#"> Upload the file contained in the myFile field. Always upload to a directory outside of the webroot, validate the file extension, file content and then only if necessary copy it back to the web root.
<cffile action="upload" destination="#getTempDirectory()#" filefield="form.myFile" nameconflict="makeunique"> CF 10+ Checks file extensions against a whitelist of allowed file extensions. You must set strict=false when specifying a file extension list.
<cffile action="upload" accept=".png,.jpg" strict="false" destination="#getTempDirectory()#" filefield="form.myFile" nameconflict="makeunique"> Upload all files in the form scope.
<cffile action="uploadall" destination="#getTempDirectory()#" nameconflict="makeunique">
© 2012–present Foundeo, Inc. and the CFDocs contributors.
Licensed under the MIT License.
ColdFusion is a trademark of Adobe Systems Incorporated.
https://cfdocs.org/cffile