Postlet Coldfusion upload handler
This is just a quick follow up on my Postlet post from yesterday. I have a simple upload handler written for Postlet, with configuration options and debug capability. Have fun ;)
<br /><br /> <!---//<br /> ***************************************************<br /> Configuration, change things here for your setup<br /> ***************************************************<br /> //---><br /><cfscript><br /> // Set upload directory here, use any<br /> // notation acceptable by expandPath()<br /> uploadDir = /assets/uploads/;<br /> <br /> // Set folder name to drop files into<br /> // You can leave this blank to put all<br /> // images into the root, or set to a variable<br /> // to use an incoming url based argument<br /> // to set to a fixed string, use = foldername<br /> // to set to a URL argument, use = url.varName<br /> // to set no sub-folder, use = <br /> uploadFolder = url.folder;<br /> <br /> // Allowable file extensions<br /> allowedExt = jpg,png,gif;<br /> <br /> // optionally check the host page and block<br /> // the request if the host is not appropriate<br /> checkHost = true;<br /> hostURL = your host name here ie google.com, microsoft.com;<br /> <br /> // If you would like debug output, set debugmode <br /> // to true, and set a file to be written with output<br /> debugMode = true;<br /> debugFile = c:\\uploadTest.html;<br /> </cfscript><br /> <br /> <!---// <br /> *************************************************** <br /> This is where things happen, you probably dont need<br /> to mess with this ;)<br /> ***************************************************<br /> //---><br /><br /> <!---// Default ret value to success //---><br /><cfset ret = success /><br /> <br /> <!---// Validate refer //---><br /><cfif checkHost AND not findNoCase(CGI.HTTP_HOST, hostURL)><br /> <cfset ret = errorInvalidHost><br /> </cfif><br /> <br /> <!---// Determine final filesystem location of files //---><br /><cfif url.folder neq ><br /> <cfset serverDirectory = expandPath(uploadDir & uploadFolder) /><br /> <cfelse><br /> <cfset serverDirectory = expandPath(uploadDir) /> <br /> </cfif> <br /> <br /> <!---// Create directory if necessary //---><br /><cfif ret eq success AND not directoryExists(serverDirectory)><br /> <cftry><br /> <cfdirectory action="create" directory="#serverDirectory#" /><br /> <br /> <cfcatch type="any"><br /> <cfset ret = errorCreatingDirectory /><br /> <cfset err = cfcatch /><br /> </cfcatch><br /> </cftry><br /> </cfif><br /> <br /> <!---// Save file if no errors this far //---><br /><cfif ret eq success><br /> <cftry><br /> <cffile action="upload" filefield="USERFILE" destination="#serverDirectory#" nameconflict="makeunique" /><br /> <br /> <cfcatch type="any"><br /> <cfset ret = errorSavingFile /><br /> <cfset err = cfcatch /><br /> </cfcatch><br /> </cftry><br /> </cfif><br /> <br /> <!---// Make sure the file is of acceptable type //---><br /><cfif ret eq success AND not listFindNoCase(allowedExt, Cffile.serverFileExt)><br /> <!---// File is not of type allowed, delete it and return error //---><br /> <cffile action="delete" file="#serverDirectory#\\#CFFile.ServerFile#" /><br /> <br /> <cfset ret = fileTypeNotAllowed /><br /> </cfif><br /> <br /><br /> <cfsavecontent variable="response"><br /> <br /> <cfswitch expression="#ret#"><br /> <cfcase value="errorCreatingDirectory,errorInvalidHost"><br /> <cfheader statuscode="500" statustext="HTTP/1.1 500 Internal Server Error"><br /> <cfcontent reset="true" /><br /> <cfoutput>POSTLET REPLY#chr(10)##chr(13)#POSTLET:NO#chr(10)##chr(13)#POSTLET:SERVER ERROR#chr(10)##chr(13)#POSTLET:ABORT THIS#chr(10)##chr(13)#END POSTLET REPLY#chr(10)##chr(13)#</cfoutput> <br /> </cfcase><br /> <cfcase value="fileTypeNotAllowed"><br /> <cfheader statuscode="403" statustext="HTTP/1.1 403 Forbidden"><br /> <cfcontent reset="true" /><br /> <cfoutput>POSTLET REPLY#chr(10)##chr(13)#POSTLET:NO#chr(10)##chr(13)#POSTLET:FILE TYPE NOT ALLOWED#chr(10)##chr(13)#POSTLET:ABORT THIS#chr(10)##chr(13)#END POSTLET REPLY#chr(10)##chr(13)#</cfoutput> <br /> </cfcase><br /> <cfdefaultcase><br /> <cfcontent reset="true" /><br /> <cfoutput>POSTLET REPLY#chr(10)##chr(13)#POSTLET:YES#chr(10)##chr(13)#END POSTLET REPLY#chr(10)##chr(13)#</cfoutput> <br /> </cfdefaultcase><br /> </cfswitch><br /> <br /> </cfsavecontent><br /><br /></cfsilent><cfoutput>#response#</cfoutput><cfsilent><br /><br /> <!---// If in debug mode, write results to a debug file //---><br /><cfif debugMode><br /> <cfsavecontent variable="debugText"><br /> <cfoutput><br /> Response: #ret#<br /><br /> Output: [#response#] <br /> <cfif isDefined("Cffile")><br /> <p><br /> <h1>File Details</h1><br /> <cfdump var="#CFFile#"><br /> </p><br /> </cfif><br /> <cfif structKeyExists(variables, "err")><br /> <p><br /> <h1>Error Details</h1><br /> <cfdump var="#err#"><br /> </p><br /> </cfif><br /> <cfif isDefined("cgi")><br /> <p><br /> <h1>CGI Details</h1><br /> <cfdump var="#cgi#"><br /> </p><br /> </cfif><br /> </cfoutput><br /> </cfsavecontent><br /> <cffile action="write" file="#debugFile#" output="#debugText#" /><br /> </cfif><br /><br /></cfsilent><br />