<cfset extList = "pdf,gif,jpg,jpeg,doc,docx,xls,xlsx,png,zip">
<CFFILE action="upload" filefield="upload" destination="#tempDir#" result="fileUpload" nameconflict="MAKEUNIQUE">
<cfif NOT fileUpload.fileWasSaved> <!--- Determine whether the image file is saved. --->
<cfthrow message="Upload file failed">
</cfif>
<cfif listFindNoCase(extList, fileUpload.clientFileExt) eq 0>
<cfthrow message="Invalid file type. File extension must be one of these: #extList#">
</cfif>
<CFIF isdefined ("FORM.UploadPDF")>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF>
<cfelse>
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
<CFIF isdefined ("FORM.UploadPDF")>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF> <!------- THIS WAS MISSING -------->
<cfelse>
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
<CFIF isdefined ("FORM.UPLOAD")>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
<CFABORT>
</CFIF>
</CFIF>
<CFIF isdefined ("FORM.UploadPDF")>
<CFIF VAL(CGI.CONTENT_TYPE) VAL PDF><!---this is where I was wondering if there's something I could add here?--->
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
</CFIF>
<CFIF isdefined ("FORM.UploadPDF")>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
<cfelse>
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
</CFIF>
<CFIF isdefined ("FORM.UploadPDF")>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF>
<cfelse>
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm"> <=== the FILE IS TOO BIG
<cfelse>
<cflocation addtoken="no" url="nopdf.htm"> <===== the FILE IS NOT FOUND
</CFIF>
==== IT WILL ENVER REACH HERE, SUCCESS IS IMPOSSIBLE
here's the code that allows for a pdf to be attached/submitted but send the user to the 404 page after submit is clicked:What happened when you just removed the line:
<CFIF isdefined ("FORM.UploadPDF")>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF>
<cfelse>
NO PDF FOUND... IS THERE STILL AN ERROR?
</CFIF>
<CFIF isdefined ("FORM.UploadPDF")>
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF>
<cfelse>
<cflocation addtoken="no" url="/nopdf.htm"> <==== add the relative path to the location... what FOLDER is it IN?
</CFIF>
<cfparam name="form.UploadPDF" default="">
<CFIF isdefined ("FORM.UploadPDF") AND Form.UploadPDF IS NOT "" >
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF>
<cfelse>
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
<cfif isdefined ("url.Action")>
<!---
Test the file size
--->
<cfif val(cgi.content_length) gt 1024000>
<!---
the file size is over the limit of 1mb. Refuse to upload it
--->
<cfset variables.Success="Too Big! No dice.">
<cfelse>
<!---
the file is within the limit specified. Define the accepted
MIME types. this example accepts plain text files and zip
files.
--->
<cfset request.AcceptPDF=
"application/pdf">
<!---
now try to upload the file
--->
<cftry>
<cffile
action="Upload"
filefield="FileContents"
destination="c:\cfusionmx\wwwroot\"
nameconflict="OVERWRITE"
accept="#request.AcceptImage#">
<cfset variables.Success="Uploaded.">
<cfcatch type="Application">
<!---
something went wrong. Was it a mime type failure?
--->
<cfif isdefined("cfcatch.MimeType")>
<!---
yes it was. show the friendly error message.
--->
<cfif not ListContains
(request.AcceptImage,cfcatch.MimeType)>
<h1>Fool!</h1>
This type of file is not allowed for upload.<br>
All that user training really paid off.</p>
<p>Try again...</p>
<cfelse>
<!---
Hmmm. the mimetype is there but the file was on the
list. Better dump out the whole error message.
--->
<cfoutput>
<b>Error</b><br>
#cfcatch.Message#
#cfcatch.Detail#
</cfoutput>
</cfif>
<cfelse>
<!---
Hmmm. No mimetype error in the catch scope.
Better dump out the whole error message.
--->
<cfoutput>
<b>Error</b><br>
#cfcatch.Message#
#cfcatch.Detail#
</cfoutput>
</cfif>
<cfabort>
</cfcatch>
<cfcatch type="Any">
<cfoutput>
<b>Error</b><br>
#cfcatch.Message#
#cfcatch.Detail#
</cfoutput>
<cfabort>
</cfcatch>
</cftry>
</cfif>
</cfif>
<!---
Display the form
--->
<html><head><title>Uploader Test</title></head><body>
<cfif isdefined ("url.Action")>
<cfoutput>
Your file was #variables.Success#
</cfoutput>
</cfif>
<cfoutput>
<form
action="#cgi.script_name#?Action=Y"
method="post"
enctype="multipart/form-data">
</cfoutput>
Source File Name:<BR>
<input
name="FileContents"
type="FILE"
size="45"><br>
<input
type="submit"
value="Upload File">
</form>
</body></html>
<CFIF isdefined ("FORM.UploadPDF") AND Form.UploadPDF IS NOT "" >
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cfif IsPDFFile("#Form.UploadPDF#")> yes
<cfelse>
no
</cfelse>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF>
<cfelse>
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
<cfif file.clientFileExt EQ 'PDF'>
Process Code
<cfelse>
<cfif fileexists(expandpath('#file.serverfile#'))>
<cffile action="delete" file="#expandpath('#file.serverfile#')#">
</cfif>
<cflocation url="nopdf.cfm">
</cfif>
file.serverFileExt
to make a double check
<CFIF isdefined ("FORM.UploadPDF") AND Form.UploadPDF IS NOT "" >
<CFIF VAL(CGI.CONTENT_LENGTH) GT 4000000>
<cflocation addtoken="no" url="filetoolarge.htm">
</CFIF>
<cfelse>
<cflocation addtoken="no" url="nopdf.htm">
</CFIF>
<cffile action="UPLOAD".....> <!--- upload the file --->
<cfif file.clientFileExt is not "PDF">
<cffile action="DELETE"... > <!---- remove the uploaded file because it is the wrong type ---->
<cflocation addtoken="no" url="notTypePdf.htm"> <!---- redirect to the error page for this ----->
</cfif>
... keep processing...
When the user submits the form, you can check any form variable to see if it is populated; maybe the name or email address is required. Same thing with the form upload field. Simply check to see if it has a value using ColdFusion before you process it to the database. If your requirements are not met, then do not save the information and return the user to the form with an error message.
<cfif len(form.lastName) eq 0>
--- process error message - last name is required
<cfif len(form.fileUpload) eq 0>
--- process error - file is required
If you want to be sure the file is a pdf, go ahead and use cffile to upload it and then check the extension on the uploaded file. Using cffile.extension (or similar, you may have to check the exact name). If it is not what you want, do not save the contents, just return an error to the user and redisplay the form.