Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / September 2007



Tip: Looking for answers? Try searching our database.

File attachment validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shearak - 05 Sep 2007 16:36 GMT
So I have a form that  allows users to attach documents by browsing for the
files. How would I check if a files exists where the user doesn't use browse to
attach a file but rather types in a file and path name (and the file isn't
found)?

<form action="Contact_Submission_Action.cfm" method="post"
enctype="multipart/form-data" name="contact_submission" id="contact_submission"
onSubmit="MM_validateForm('Email','','RisEmail');return
document.MM_returnValue">
          <table width="815" border="0" align="center" class="submission_form">
              <tr>
                  <td colspan="10">&nbsp;</td>
              </tr>
              <tr>
                  <td colspan="2" class="fieldheaderleft">Your e-mail
address:<br><span class="note">(required)&nbsp;</span></td>
                  <td colspan="8"><input type="text" name="Email" size="50"
value=""></td>
              </tr>  
          <tr>
                  <td colspan="2" class="fieldheaderleft">Date to publish this
item: <span class="note">(required)&nbsp;</span></td>
                  <td colspan="8"><input type="text" name="date" value="
mm/dd/yy"></td>
              </tr>
         <tr>
                  <td colspan="2" class="fieldheaderleft">Headline:</td>
                <td colspan="8"><input type="text" name="Headline" size="66"
value=""></td>
              </tr>             
              <tr>
                  <td colspan="2" class="fieldheaderleft">Attachment #1:</td>
                  <td colspan="8"><input type="file" name="attachment_1"
size="50" value=""></td>
              </tr>             
          <tr>
                  <td colspan="2" class="fieldheaderleft">Attachment #2:</td>
                  <td colspan="8"><input type="file" name="attachment_2"
size="50" value=""></td>
              </tr>         
             <tr>
                  <td colspan="2" class="fieldheaderleft">Attachment #3:</td>
                  <td colspan="8"><input type="file" name="attachment_3"
size="50" value=""></td>
              </tr>         
         <tr>
                  <td colspan="2" class="fieldheaderleft">Description of
related item (text for the link):</td>
                  <td colspan="8"><textarea name="WhatAdditional" cols="50"
rows="2" id="WhatAdditional"></textarea></td>
              </tr>
         <tr>
                  <td colspan="2" class="fieldheaderleft">Comments or
additional instructions for this item:</td>
                  <td colspan="8"><textarea name="Comments" cols="50" rows="6"
wrap="VIRTUAL" id="Headline"></textarea></td>
              </tr>
        <tr>
                  <td colspan="2">&nbsp;</td>
                  <td colspan="8"><input type="submit" name="Send_ Email"
value="Submit News Item"><input name="Clear form" type="reset" value="Clear
Form"></td>
              </tr>
          </table>
</form>  

Thanks in advance,
Grizzly9279 - 05 Sep 2007 18:13 GMT
I think the best you can do is attempt to upload the file (using <cffile
action="upload">).  If the file did not exist on the client machine, <cffile>
should throw an error which says something like:

"The form field "xxxxxxx" did not contain a file."

You can wrap some try/catch logic around the <cffile action="upload"> tag so
you can detect this condition and treat it as you best see fit.
shearak - 05 Sep 2007 19:02 GMT
Here is my action page using <cffile action="upload">. The upload works fine if
the user browes and attaches the files. But if the user types in a file and
path name (and the file isn't found) I get an error. Can someone show me how I
can validate if the file/s do not exist.

<cfset dir = ExpandPath("#request.app.Attach#/")>
<cfif FORM.attachment_1 neq "">
       <!--- first actually upload the file --->
       <cffile action="upload"
                 filefield="attachment_1"
                 destination="#dir#/"
                 nameconflict="Makeunique">
        <!--- now create a temporary holder for the attachment later on --->
        <cfset attachment_local_file_1 = "#dir#/#file.serverfile#">
</cfif>
<cfif FORM.attachment_2 neq "">
       <!--- first actually upload the file --->
       <cffile action="upload"
                 filefield="attachment_2"
                 destination="#dir#/"
                 nameconflict="Makeunique">
        <!--- now create a temporary holder for the attachment later on --->
        <cfset attachment_local_file_2 = "#dir#/#file.serverfile#">
</cfif>
<cfif FORM.attachment_3 neq "">
       <!--- first actually upload the file --->
       <cffile action="upload"
                 filefield="attachment_3"
                 destination="#dir#/"
                 nameconflict="Makeunique">
        <!--- now create a temporary holder for the attachment later on --->
        <cfset attachment_local_file_3 = "#dir#/#file.serverfile#">
</cfif>

Thank you for your submission!  Your news item has been sent to .  A member of
the group will contact you with any questions or to review your item.

<cfmail from="#form.email#" to="" subject="BS&R News Item Submission Form">
E-Mail: #Email#
News Section: #News#
Date to publish this item: #date#
Headline: #headline#
News item text: #NewsItem#

Attachment 1: #A1#
Description 1: #Description1#
Attachment 2: #A2#
Description 2: #Description2#
Attachment 3: #A3#
Description 3: #Description3#
     
     
Link to related information (if any): #RelateLink#
What is this link? #WhatLink#
Additional link to related information (if any): #AdditionalLink#
What is this additional link? #WhatAdditional#

Comments or additional instructions for this item:
-------------------------------------------------------
#Comments#

-------------------------------------------------------
   
   <cfsilent>
     <cfif FORM.attachment_1 neq "">
        <cfmailparam file="#attachment_local_file_1#">
     </cfif>
     <cfif FORM.attachment_2 neq "">
        <cfmailparam file="#attachment_local_file_2#">
     </cfif>
     <cfif FORM.attachment_3 neq "">
        <cfmailparam file="#attachment_local_file_3#">
     </cfif>
   </cfsilent>
</cfmail>

Thanks again!
shearak - 06 Sep 2007 20:47 GMT
Dump! Any suggestions please.
Ian Skinner - 05 Sep 2007 18:46 GMT
Have you tested if this even gets past the browser?  Since it has to get
the file and put it in the header of the request to the server, I would
presume that if the browser can not find the file it is going to fail at
that point.  But I have never tested this so I do not know what the
common behavior of browsers would be for uploading a non-existing file.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.