Exercise 7: 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. This business rule has two parts:

You used the required attribute for the photo cfinput tag to ensure that a filename is entered. Now you must ensure that the file exists in the right directory so that the application can display it to the user.

Because browser clients are prohibited from doing standard file input and output (I/O) on the web server, the Trip Maintenance application uses server-side validation to ensure the existence of the photo file. You add the business rule for the photo file to the tripeditaction.cfm page.

To verify that a file exists, ColdFusion provides a FileExists function. This function has the following syntax:

FileExists(absolute_path)

This function returns Yes if the file specified in the argument does exist; otherwise, it returns No.

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, the photos are stored in the C:\inetpub\wwwroot\cfdocs\getting_ started\photos directory.

For more information about the FileExists function, see CFML Reference.

To verify that the photo filename exists:

  1. Open the tripeditaction.cfm file in the my_app directory.
  2. Add logic to check that the user entered a valid photo filename by copying the following code immediately following the first <cfset isOk = "Yes"> statement:
    	<!-- Check to see if photo file exists. --->
    	<cfset PhotoLocation = "C:\Inetpub\wwwroot\CFDOCS\getting_started\Photos\">
    	<cfset PhotoLocation = PhotoLocation  & FORM.photo> 
    	<cfif not FileExists(PhotoLocation)> 
    	   <cfset isOk = "No">
    	   <cfoutput>Trip Photo does not exist</cfoutput>
    	</cfif>
    
  3. Verify that the code you just copied is pointing to the correct PhotoLocation path. The path is specified in the <cfset PhotoLocation = "C:..."> tag.

    For example, depending on your web server configuration, the PhotoLocation path might be:

  4. Save the file.

To test the modified code:

  1. View the tripedit.cfm page in your browser.
  2. Perform tests by doing the following:
    1. In the Trip Edit page, enter valid information in all the required fields except the Photo Filename field.
    2. In the Photo Filename field, enter nowhere.jpg, and click Save.

      The following error message appears: Trip photo does not exist.

    3. To avoid the error, replace the invalid photo filename in the Trip Edit page with somewhere.jpg and click Save.

      The following message appears: Trip added.

Reviewing the code

The following table describes the code used to verify whether the photo file exists:

Code

Explanation

<cfset PhotoLocation =
"C:\CFusionMX7\wwwroot\cfdocs\
getting_started\photos\">

The cfset tag sets the value of the PhotoLocation path to the appropriate directory.

Note: The code snippet shows one value for Windows systems.

<cfset PhotoLocation = PhotoLocation  & FORM.photo>

The ColdFusion & operator in the cfset tag combines the directory name with the name of the photo file entered in the form.

<cfif not FileExists(PhotoLocation)> 
	<cfset isOk = "No">
	<cfoutput>
	Trip Photo does not exist.
	</cfoutput>
</cfif>

FileExists checks to see if the file indicated by the fileLocation variable exists at the specified disk location. If it doesn’t, an error message appears.