Link to home
Start Free TrialLog in
Avatar of jturkington
jturkington

asked on

Validating Filename Being Uploaded

I am still learning the concept of file uploads. Can someone show me in the right direction on this one.

I have a unique variable on the page called VARIABLES.Filename (DOC001.doc etc..) that i need to compare with the filename being uploaded with the input file field.

I need to perform some server side validation such as

<cfif VARIABLES.Filename NEQ filename?????>
         Filename being attached doesn't match DOC001.doc  
</cfif>

Can someone tell me the best way to handle this

Best regards

JT
ASKER CERTIFIED SOLUTION
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of section25
section25

I just answered almost the same question a few minutes ago. Here is the link to that question.

https://www.experts-exchange.com/questions/21403286/display-an-image-outside-of-the-cold-fusion-web-root.html

Basically, what you want to do is grab the file name as the page is being uploaded, and then compare that on the next page.

Here is the code again (modified for your question):

Page 1
------
<SCRIPT LANGUAGE="JavaScript">
 function get_name() {
  var test_array = document.form1.filecontents.value.split("\\");
  var x = test_array.length;
  document.form1.file_name.value=test_array[x-1];
  form1.submit();
 }
</SCRIPT>

<form action="file_upload.cfm" enctype="multipart/form-data" method="post" name=form1>
 Enter the file: <input name="filecontents" type="file" size=60>
 <input name="file_name" type="hidden"><br><br>
 <button onClick=get_name();>Submit</button>
</form>

Page 2 (file_upload.cfm)
------------------------
<!-- compare the file name -->
<cfif variables.filename eq #form.file_name#>
 <!-- do something -->
</cfif>
That is not server side validation and supposing that jaavscript is turned off will not work.