Products

Products
Viewing By Entry / Main
September 28, 2007

cffile upload test source code

Just a quick example here of a script that helps you to understand file uploads and a few other things. Look in the documentation for more tricks.

It presents you with a form to get a file on your local machine and click submit to upload it to whatever directory you want it stored on your server.

Then it will show you the temp directory it stored it to before moving it to the target directory. It will also show you a dump of the file upload result and the directory where you're uploading files to.

You can set file size limits and file extension type limits. It will also output a list of the uploaded files and compare it to a predetermined date and time.

<cfparam name="fileDirectory" default="c:\path\to\your\directory\"/>
<cfparam name="form.file" default=""/>
<cfparam name="fileSuccess" default="Error"/>
<cfparam name="fileSizeLimitKB" default="500"/>
<cfparam name="fileExtAllowed" default="doc,txt,gif,jpg,jpeg,pdf,png,tif,tiff,mpg,mpeg,mp3,wav,zip,rar,xls,csv"/>
<cfparam name="fileDateCompare" default="#dateAdd('n',-2,now())#"/>
<cfif form.file neq "">
   <cfoutput>Uploaded temp location: #form.file#<br /></cfoutput>
   <!--- calculate file size limit from param --->
   <cfset sizelimit = fileSizeLimitKB * 1000>
   <!--- upload file and get results --->
   <cffile action="upload"
      filefield="file"
      destination="#fileDirectory#"
      nameconflict="makeunique"
      result="uploadResult">

   <!--- check if file upload save was successful --->
   <cfif uploadResult.fileWasSaved>   
      <!--- set param of uploaded file path in case of delete --->
      <cfset newFile = uploadResult.serverDirectory & "/" & uploadResult.serverFile>
      <!--- check for size and unsafe file extensions --->
      <cfif uploadResult.fileSize lte sizelimit and findNoCase(uploadResult.serverFileExt,fileExtAllowed) gt 0>
         <cfdump var="#uploadResult#" label="Upload Result" expand="no"/>
         <cfset fileSuccess = "Success!">
      <cfelse>
         <!--- safe file test failed, delete file --->
         <cffile action="delete" file="#newFile#"/>
         <!--- set upload msg output to correct reason --->
         <cfif findNoCase(uploadResult.serverFileExt,fileExtAllowed) eq 0>
            <cfset fileSuccess = "File type #uploadResult.serverFileExt# not allowed.">
         <cfelseif uploadResult.fileSize gt sizelimit>
            <cfset fileSuccess = "File size #uploadResult.fileSize/1000# exceeds limit of #fileSizeLimitKB# Kb.">
         </cfif>
      </cfif>
   <!--- if all else fails --->
   <cfelse>
      <cfset fileSuccess = "Error uploading file. Please try again.">
   </cfif>
      <!--- get query list of files in uploaded directory location --->
      <cfdirectory action="list"
         name="qDir"
         directory="#uploadResult.serverDirectory#"/>

      <cfdump var="#qDir#" label="Directory List" expand="no"/>
   <!--- output file upload success msg --->
   <cfoutput>#fileSuccess#</cfoutput>
</cfif>

<html>
<head>
<title>Upload file</title>
</head>

<body>

<!--- upload form --->
Select a file to upload:<br />
<form method="post" action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" enctype="multipart/form-data">
   <input type="file" name="file" id="file" size="40"/>
   <input type="submit" name="button" id="button" value="Submit" />
</form>
<!--- show upload limits --->
<cfoutput>
   Limitations -<br />
   Size: #fileSizeLimitKB# Kb<br />
   Extensions: #fileExtAllowed#
</cfoutput>
<!--- output directory and do a date compare (could delete older files if needed) --->
<cfif form.file neq "">
   <br /><br />
   <strong>Date compare:</strong><br />
   <cfoutput query="qDir">
      #currentRow#. #name# is <cfif dateCompare(dateLastModified,fileDateCompare) lte 0>older<cfelse><strong>younger</strong></cfif> than #fileDateCompare#.
      <br />
   </cfoutput>
</cfif>
</body>
</html>

Comments

There are no comments for this entry.


Copyright © 2005-2006 Clint Willard. All rights reserved.
Aura skin for Clint Willard's BlogCFC inspired by Brooks Bilson's Bolg.
All trademarks property of their owners.