Welcome to the very first of my ColdFusion 8 blog postings. (Well, the first since the public release.) My goal for these entries is to look at features, big and small, and show some practical examples. For my first entry, I'm going to talk about something simple - validating that a file uploaded is an image.

As you probably heard, ColdFusion 8 has about 900 or so image functions. Ok, it isn't quite that much, but there are quite a few of them. (By the way, I'm speaking on image features at CFUNITED.) Probably the most common thing you will need to do is simple validation on a file upload. What do I mean by that?

Imagine a preferences form. It asks you things like your name, email address, and other items. It also lets you upload a picture of yourself. How do you validate that the file is an image? After the file is uploaded, it takes all of one call:

<cfif not isImageFile(newfile)>

The isImageFile function simply checks and see if a filename points to an image file that ColdFusion can work with. Here is a slightly larger example:

<cfif not len(trim(form.picture))> <cfset errors = errors & "Avast Ye! Include a picture or walk the plank!<br />"> <cfelse> <cffile action="upload" destination="#expandPath('./images')#" nameConflict="makeunique" filefield="picture" result="fileupload"> <cfif fileupload.fileWasSaved> <cfset newfile = fileupload.serverdirectory & "/" & fileupload.serverfile>

<cfif not isImageFile(newfile)> <cfset errors = errors & "Avast Ye! Include a VALID picture or walk the plank!<br />"> <!--- clean up ---> <cffile action="delete" file="#newfile#"> </cfif> </cfif> </cfif>

In this code block, I not only check and see if the user selected something to upload, I also handle the upload, check to see if it is an image, and even handle the cleanup if not. Note the special "Pirate" mode for errors. I love that.

A complete example is included below. Tomorrow I'll follow this up with a simple size check. That would be useful to for preventing users from upload 2 meg pictures or overly large wide/high pictures.

<cfset errors = ""> <cfparam name="form.name" default=""> <cfparam name="form.picture" default="">

<cfif structKeyExists(form, "save")> <cfif not len(trim(form.name))> <cfset errors = errors & "Avast Ye! Include a name or walk the plank!<br />"> </cfif>

<cfif not len(trim(form.picture))> <cfset errors = errors & "Avast Ye! Include a picture or walk the plank!<br />"> <cfelse> <cffile action="upload" destination="#expandPath('./images')#" nameConflict="makeunique" filefield="picture" result="fileupload"> <cfif fileupload.fileWasSaved> <cfset newfile = fileupload.serverdirectory & "/" & fileupload.serverfile>

<cfif not isImageFile(newfile)> <cfset errors = errors & "Avast Ye! Include a VALID picture or walk the plank!<br />"> <!--- clean up ---> <cffile action="delete" file="#newfile#"> </cfif> </cfif> </cfif>

<cfif errors is ""> <cfoutput> <p> Here is where we would update the database and send the user away... </p> </cfoutput> <cfabort> </cfif>

</cfif>

<cfif errors neq ""> <cfoutput> <p> <b>Please correct the following error(s):<br /> #errors# </b> </p> </cfoutput> </cfif>

<cfoutput> <form action="imageuploadform.cfm" method="post" enctype="multipart/form-data"> <table> <tr> <td>Your Name:</td> <td><input type="text" name="name" value="#form.name#"></td> </tr> <tr> <td>Your Picture:</td> <td><input type="file" name="picture"></td> </tr> <tr> <td> </td> <td><input type="submit" name="save" value="Save"></td> </tr> </table> </form> </cfoutput>