Programming instructions

Developing code to validate data and enforce business rules 115
Validating the existence of the trip photo file
At this point, you have a more efficient application. The client is handling much of the
validation of the Compass Travel new trip business rules. Except for the trip photo file,
the server receives only valid data.
The trip photo file business rule does not fit nicely into this design, however. The last trip
photo business rule has two parts:
A photo file name must accompany all new trips.
The photo image file must reside within the images directory of the Compass Travel
website.
You used the
required attribute for the photo cfinput tag to ensure that a file name 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 file name, 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.