User Guide Cancel

FileRead

 

Note:

You can find the CFFiddle demo of this function and other file functions as part of a project that is shared with you.

Click the button below to launch CFFiddle.

To copy the project in your workspace in CFFiddle, follow the steps below:

  1. Log in with your Gmail or Facebook credentials.
  2. Navigate to the project in the left pane.
  3. Once you make some changes in any cfm in the project, a pop up displays asking you to save the project.
  4. Give the project a suitable name and click Save.
  5. Create a folder named dir1 and upload a text file, myfile.txt.

Description

Reads an on-disk or in-memory text file or a file object created with the FileOpen function. You use this function either as an alternative to the cffile tag with the action="read" attribute. You can also use this function to improve performance when reading a large file, because FileRead does not read the entire file into memory.

Returns

If you specify a filepath, the full text content of the file.
If you specify a file object, the character or byte buffer of the specified size. If the file was opened in read mode, FileRead returns the character data (a string), otherwise it returns binary data.

Category

System functions

Function syntax

FileRead(filepath [, charset])

OR

FileRead(fileobj [, buffersize])

See also

FileCloseFileIsEOFFileReadBinaryFileReadLineFileWrite

History

ColdFusion 8: Added this function.

Parameters

Parameter

Description

filepath

An absolute path to an on-disk or in-memory text file on the server.

charset

The character encoding in which the file contents is encoded. The following list includes commonly used values:

  • utf-8
  • iso-8859-1
  • windows-1252
  • us-ascii
  • shift_jis
  • iso-2022-jp
  • euc-jp
  • euc-kr
  • big5
  • euc-cn
  • utf-16
    If the file starts with a byte order mark and you set this attribute to a conflicting character encoding, ColdFusion generates an error.

fileobj

The file object from which to read.

buffersize

The number of characters to read.

Usage

You can read a text file or a file object with the FileRead function. When you specify an absolute path of a text file, ColdFusion reads the entire contents of the file. When you specify a file object, which you created using the FileOpen function, ColdFusion reads the number of characters specified in buffersize.

Example

<h3>FileRead Example - Reading a file</h3>

<!--- This reads and outputs the entire file contents. --->
<cfscript>
myfile = FileRead("c:\temp\myfile.txt");
WriteOutput("#myfile#");
</cfscript>

<!--- This reads and outputs the first 100 characters --->
<!--- from the same file. --->
<cfscript>
myfile = FileOpen("c:\temp\test1.txt", "read");
x = FileRead(myfile, 100);
WriteOutput("#x#");
FileClose(myfile);
</cfscript>

 

Get help faster and easier

New user?