Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active May 6, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/524b68b4fbbbf884da7f631e697defbd to your computer and use it in GitHub Desktop.
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
<!--- 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