Spec-Zone.ru › ColdFusion

cffile

Manages interactions with server files. Different combinations cause different attributes to be required.

<cffile action="read">

fileRead(path);

  • CF2+
  • Lucee
  • BL 1.0.0+
  • See Also: fileread filewrite fileopen filereadline

    Attribute Reference

    action string
    Required

    Type of file manipulation that the tag performs.
    Values:
    • append
    • copy
    • delete
    • move
    • read
    • readbinary
    • rename
    • upload
    • uploadall
    • write

    file string

    Pathname of the file.

    mode string

    Applies only to UNIX and Linux. Permissions. Octal values of Unix chmod command. Assigned to owner, group, and other, respectively.

    output string

    String to add to the file

    addnewline boolean
    Default: true

    Yes: appends newline character to text written to file
    Values:
    • true
    • false

    attributes string

    Applies to Windows. A comma-delimited list of attributes to set on the file. If omitted, the file's attributes are maintained.
    Values:
    • readonly
    • hidden
    • normal
    • system
    • temporary

    charset string

    The character encoding in which the file contents is encoded. For more information on character encodings, see: www.w3.org/International/O-charset.html.
    Values:
    • utf-8
    • iso-8859-1
    • windows-1252
    • us-ascii
    • shift_jis
    • iso-2022-jp
    • euc-jp
    • euc-kr
    • big5
    • euc-cn
    • utf-16

    source string

    Pathname of the file (during copy).

    destination string

    Pathname of a directory or file on web server (during copy).

    variable string

    Name of variable to contain contents of text file.

    filefield string

    Name of form field used to select the file. Do not use pound signs (#) to specify the field name.

    nameconflict string

    Action to take if filename is the same as that of a file in the directory.
    Values:
    • error
    • skip
    • overwrite
    • makeunique

    accept string

    Limits the MIME types to accept. Comma-delimited list. For example, to permit JPG and Microsoft Word file uploads: accept = "image/jpg, application/msword"

    result variableName
    Default: cffile

    Allows you to specify a name for the variable in which cffile returns the result (or status) parameters. If you do not specify a value for this attribute, cffile uses the prefix "cffile".

    fixnewline string
    Default: false

    * Yes: changes embedded line-ending characters in string variables to operating-system specific line endings * No: (default) do not change embedded line-ending characters in string variables.
    Values:
    • true
    • false

    cachedwithin numeric

    Lucee 5+ Timespan, using the CreateTimeSpan function. If original file date falls within the time span, cached file data is used. CreateTimeSpan defines a period from the present, back.

    Compatibility

    BoxLang:

    Version 1.0.0+ <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

    Script Syntax - Write

    File Write

    fileWrite(expandPath("./myFile.txt"), "Here's some content for my file.");

    Script Syntax - Append

    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);

    Script Syntax - Read

    File Read

    myFile = fileRead(expandPath("./myFile.txt"));

    Script Syntax - Read Binary

    File Read Binary

    myImageBinary = fileReadBinary(expandPath("./myImage.jpg"));

    Script Syntax - Rename

    File Rename - Since there is no fileRename(), fileMove() works just as well

    fileMove(expandPath("./myFile.txt"), expandPath("./myNewFileName.txt"));

    Script Syntax - Copy

    File Copy

    fileCopy(expandPath("./myFile.txt"), expandPath("./some/other/path"));

    Script Syntax - Move

    File Move

    fileMove(expandPath("./myFile.txt"), expandPath("./some/other/path"));

    Script Syntax - Delete

    File Delete

    fileDelete(expandPath("./myFile.txt"));

    Script Syntax - Upload

    File Upload

    fileUpload(getTempDirectory(), "form.myFile", " ", "makeunique");

    Script Syntax - Upload All

    File Upload All

    fileUploadAll(getTempDirectory(), " ", "makeunique");

    Tag Syntax (action=write)

    Write the contents of a variable to a file.

    <cffile action="write" file="#expandPath("./myFile.txt")#" output="Here's some content for my file.">

    Tag Syntax (action=append)

    Append content to the end of a file.

    <cffile action="append" file="#expandPath("./myFile.txt")#" attributes="normal" output="Here's some new content.">

    Tag Syntax (action=read)

    Read a file into a variable

    <cffile action="read" file="#expandPath("./myFile.txt")#" variable="myFile">

    Tag Syntax (action=readBinary)

    File Read Binary

    <cffile action="readBinary" file="#expandPath("./myImage.jpg")#" variable="myImageBinary">

    Tag Syntax (action=rename)

    Rename a file

    <cffile action="rename" source="#expandPath("./myFile.txt")#" destination="#expandPath("./myNewFileName.txt")#" attributes="normal">

    Tag Syntax (action=copy)

    Copy a file

    <cffile action="copy" source="#expandPath("./myFile.txt")#" destination="#expandPath("./some/other/path")#">

    Tag Syntax (action=move)

    Move a file

    <cffile action="move" source="#expandPath("./myFile.txt")#" destination="#expandPath("./some/other/path")#">

    Tag Syntax (action=delete)

    Delete a file

    <cffile action="delete" file="#expandPath("./myFile.txt")#">

    Tag Syntax (action=upload)

    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">

    Tag Syntax (action=upload) with accept

    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">

    Tag Syntax (action=uploadall)

    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

    Spec-Zone.ru

    Настройки Оффлайн Что нового Помощь О нас
    Spec-Zone .ru
    спецификации, руководства, описания, API