Manages interactions with server files.
Different combinations cause different attributes to be
required.
<cffile action="read">
fileRead(path);
append
copy
delete
move
read
readbinary
rename
upload
uploadall
write
true
true
false
readonly
hidden
normal
system
temporary
utf-8
iso-8859-1
windows-1252
us-ascii
shift_jis
iso-2022-jp
euc-jp
euc-kr
big5
euc-cn
utf-16
error
skip
overwrite
makeunique
cffile
false
true
false
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">
Signup for cfbreak
to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.