Skip to main content
Known Participant
April 5, 2010
Question

cffile upload and type="file" ... how do I get form value ...

  • April 5, 2010
  • 2 replies
  • 1873 views

<input class="pfs" type="File" name="file_name" size="120" maxlength="100">

I send this to a cffile type="upload" ... and I am trying to do a special error handler ...

If file exists on server, first move it to an archive folder, then upload new one to destination folder.

BUT the form name of file_name - nowhere does it contain the file name, instead ...

C:\CFusionMX7\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp41631.tmp

Hummmm.  Never tried this b4 ... how DO I get the file name!?

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
April 11, 2010

Here is a fully worked out example. It requires you to first create the directories c:\archive and c:\uploads.

I am on Coldfusion 8. In the case where an error is thrown, Coldfusion stores the name of the existing file as cfcatch.dest. I wonder whether that will be the same in MX7.

<!---

When there is no name conflict, the file is uploaded to c:\uploads.

The code within cfcatch doesn't run.

When there is a name conflict, Coldfusion throws an error.

The code within cfcatch runs.

The existing file is moved to c:\archive.

The current file is uploaded to c:\uploads

--->

<cfif isDefined("Form.FileContents") >

    <cftry>

        <cffile action = "upload"

            fileField = "FileContents"

            destination = "c:\uploads" 

            nameConflict = "error">

            File successfully uploaded.

    <cfcatch type="application">

        <!--- Full path to existing file of same name as uploaded file --->

         <cfset fullPathOfExistingFile = cfcatch.dest>

        <!--- Move existing file to archive --->

        <cffile

            action = "move"

            source = "#fullPathOfExistingFile#"

            destination = "c:\archive">

        File successfully moved.<br>

       <!--- upload file --->

        <cffile action = "upload"

             fileField = "FileContents"

             destination = "c:\uploads" 

             nameConflict = "overwrite">

        File successfully uploaded.

    </cfcatch>

    </cftry>

<cfelse>

    <form method="post" action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" enctype="multipart/form-data">

        <input name="FileContents" type="file">

        <br>

        <input name="submit" type="submit" value="Upload File">

    </form>

</cfif>

ilssac
Inspiring
April 5, 2010

Yes that is where the web server put the file and told ColdFusion so ColdFusion can do what it wants with it.

The documentation has some good information that would probably help you.

But you may want to explore the <cfdump var=#cffile#> structure.

Also, the <cffile ...> tag is properties that can help with name collisions.

To do what you want my require a try/catch or temporaray name, move and rename approach.