User Guide

Developing code to validate data and enforce business rules 107
You used the required attribute for the photo cfinput tag to ensure that a filename is entered.
Now you must make sure that the file exists in the right directory so the application can display it
to customers.
Since browser clients are prohibited from doing standard file I/O (input/output) on the web
server, the Trips Maintenance application will use server-side validation to ensure the existence of
the photo file. You will add the business rule for the photo file to the tripeditaction.cfm page.
To check to see if a file exists, ColdFusion provides a
FileExists function. The syntax of this
function is:
FileExists(absolute_path)
This function returns True if the file specified in the argument does exist; otherwise, it returns
False. Assume that a data entry page has an input tag named "testFileName". If a user types in a
valid filename, the action page snippet from the following example would display the message
"The test file exists":
<cfset fileLocation = "c:\inetpub\wwwroot\images\">
<cfif IsDefined("form.testFileName")>
<cfif form.testFileName is not "">
<!---Concatenate the File Location with the FileName passed in--->
<cfset fileLocation = fileLocation & form.testFileName>
<cfif FileExists(fileLocation)>
<cfoutput> The test file exists. </cfoutput>
</cfif>
</cfif>
</cfif>
Note: The trip photo images are stored in the following path relative to your web root directory:
\cfdocs\getting_ started\photos. Therefore, if your web root is C:\inetpub\wwwroot, then the photos
are stored in the C:\inetpub\wwwroot\cfdocs\getting_ started\photos directory.
For more information about the FileExists function, see CFML Reference.
Reviewing the code
The following table describes the code used to verify whether a file exists:
Exercise: use FileExists function to verify the existence of photo filename
In this exercise, you will use the Cold Fusion
FileExists function to ensure that the photo
filename entered in the Trip Edit page exists in the location specified.
Code Explanation
<cfif
IsDefined("form.testFileName")>
The cfif tag checks to see if the form variable testFileName
has been entered.
<cfset fileLocation = fileLocation
& form.testFileName>
The ColdFusion & operator in the cfset tag combines the
original value for
fileLocation from the first source line
("c:\inetpub\wwwroot\images\") with the value of the
testFileName form variable.
<cfif FileExists(fileLocation)>
FileExists checks to see if the file indicated by the
fileLocation variable exists at the specified disk location.