Validating Filename Being Uploaded

Posted on 2005-04-26
Last Modified: 2013-12-24
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  

Can someone tell me the best way to handle this

Best regards

Question by:jturkington
    LVL 35

    Accepted Solution

    Well you have to upload the file first to be able to do the comparison - then delete if it isn't valid.

    This may be helpful

    It has an electronic chapter on using the cffile tag.

    Of interest is the ClientFile attribute that is exposed.

    This may help too:
    Look under the heading "Evaluating the results of a file upload"
    LVL 8

    Expert Comment

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

    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;

    <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>

    Page 2 (file_upload.cfm)
    <!-- compare the file name -->
    <cfif variables.filename eq #form.file_name#>
     <!-- do something -->
    LVL 35

    Expert Comment

    That is not server side validation and supposing that jaavscript is turned off will not work.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
    Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now