Last active
May 6, 2022 15:55
-
-
Save JamoCA/524b68b4fbbbf884da7f631e697defbd to your computer and use it in GitHub Desktop.
getFormFiles UDF: Return a struct with all form field & file data from a form post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 2022-05-05: getFormFiles UDF for Adobe ColdFusion and Lucee | |
Gist: https://gist.github.com/JamoCA/524b68b4fbbbf884da7f631e697defbd | |
Blog: https://dev.to/gamesover/identifying-random-uploaded-form-files-57n7 | |
---> | |
<cfscript> | |
public struct function getFormFiles() output=false hint="I return a struct with all form field & file data from a form post" { | |
if (cgi.request_method neq "post") return {}; | |
local.result = [:]; | |
local.isLucee = server.ColdFusion.ProductName is "lucee"; | |
local.tmpPartsArray = (local.isLucee) ? form.getRaw() : form.getPartsArray(); | |
if (local.keyExists("tmpPartsArray")) { | |
local.javaFile = createObject("java", "java.io.File"); | |
for ( local.tmpPart in local.tmpPartsArray ) { | |
local.tempPath = form[local.tmpPart.getName()]; | |
local.isLuceeFile = local.isLucee && listLast(local.tempPath,".") is "upload" && fileExists(local.tempPath); | |
if ( local.isLuceeFile || (!local.isLucee && local.tmpPart.isFile())) { | |
local.data = [:]; | |
if (local.isLucee){ | |
local.data["name"] = GetPageContext().formScope().getUploadResource(local.tmpPart.getName()).getName(); | |
} else { | |
local.data["name"] = local.tmpPart.getFileName(); | |
} | |
local.filesize = local.javaFile.init(javacast("string", local.tempPath)); | |
local.data["size"] = local.filesize.length(); | |
local.data["type"] = fileGetMimeType(local.tempPath); | |
local.data["tempPath"] = local.tempPath; | |
local.result[local.tmpPart.getName()] = local.data; | |
} | |
} | |
} | |
return local.result; | |
} | |
</cfscript> | |
<cfif cgi.request_method is "post"> | |
<cfdump var=#getFormFiles()# label="getFormFiles()"> | |
</cfif> | |
<cfoutput> | |
<form action="#CGI.script_Name#<cfif len(CGI.Query_String)>?#CGI.Query_String#</cfif>" method="POST" enctype="multipart/form-data"> | |
<input type="hidden" name="fieldToIgnore" value="1"> | |
File <input type="file" name="RandomFile_#getTickCount()#" required> | |
<button type="submit">Upload</button> | |
</form> | |
</cfoutput> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment