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








Recent Comments