User Guide Cancel

FileClose

 

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

Closes an on-disk or in-memory file that is open. When you use the FileOpen function, ColdFusion returns a handle to a file object .When you close the file, the handle is still available; however, it lists the file as closed.

Category

System functions

Function syntax

FileClose(fileObj)

See also

FileCopyFileIsEOFFileOpenFileReadFileReadLineFileWrite

History

ColdFusion 8: Added this function.

Parameters

Parameter

Description

fileobj

The file to close.

Usage

Always close a file after opening it. When you use the FileOpen function to open a file, the file stream is opened and contents are read from or written to it. The FileClose function closes the stream. If you do not close a file, the stream remains open; in that case, the operating system can lock the file, which results in the file not being usable until the server is restarted.

Example

The following example checks to see if a file is still open and closes it.

<cfscript>
myfile = FileOpen("c:\my\custom\location\test1.txt", "read");
while(NOT FileIsEOF(myfile))
{
x = FileReadLine(myfile);
WriteOutput("#x# <br>");
}
</cfscript>
<!--- Additional code goes here. --->
<cfif #myfile.status# IS "open">
<cfoutput>The file #myfile.filepath# is #myfile.status#</cfoutput> <br>
<cfscript>
FileClose(myfile);
</cfscript>
</cfif>
<cfscript> myfile = FileOpen("c:\my\custom\location\test1.txt", "read"); while(NOT FileIsEOF(myfile)) { x = FileReadLine(myfile); WriteOutput("#x# <br>"); } </cfscript> <!--- Additional code goes here. ---> <cfif #myfile.status# IS "open"> <cfoutput>The file #myfile.filepath# is #myfile.status#</cfoutput> <br> <cfscript> FileClose(myfile); </cfscript> </cfif>
<cfscript> 
    myfile = FileOpen("c:\my\custom\location\test1.txt", "read"); 
    while(NOT FileIsEOF(myfile)) 
    { 
    x = FileReadLine(myfile); 
    WriteOutput("#x# <br>"); 
    } 
</cfscript> 
<!--- Additional code goes here. ---> 
<cfif #myfile.status# IS "open"> 
    <cfoutput>The file #myfile.filepath# is #myfile.status#</cfoutput>        <br> 
    <cfscript> 
        FileClose(myfile); 
    </cfscript> 
</cfif>

Get help faster and easier

New user?