views:

1238

answers:

7

How can I get the filename of a file before I call the

<cffile action = "upload">

? I can get the filename of the temp file, but not of the actual filename. In PHP land I can use the $_FILES superglobal to get what I want - but as far as I can tell no such thing exists in coldfusion.

I can get the filename client-side but would really want to do this server side.

Thanks

+7  A: 

I don't know of a way to find out before calling cffile, but there may be a workaround.

When you call <cffile action="upload"> you can specify a result using result="variable". So, call the upload with the destination as a temp file. Your result variable is a struct which contains the member clientFile, which is the name of the file on the client's computer.

Now, you can use <cffile action="move"> to do whatever it is you need to do with the original filename.

Ben Doom
Thanks for the answer... We considered this solution as it seems to be the only way. I'll leave this question open though for further answers... But I don't think we can get much better then what you posted.
nlaq
A: 

If you have the name attribute defined on the input control, the file name will be in the FORM scope. For example:

<cfif not structIsEmpty(form)>
    <cfdump var="#form#">
<cfelse>
    <html>
    <head>
     <title>Title</title>
    </head>
    <body>
     <form method="POST" action="#cgi.SCRIPT_NAME#">
      <input type="file" name="fileIn" />
      <input type="Submit" name="formSubmit">
     </form>
    </body>
    </html>
</cfif>
Jon Dowdle
The gives us the tmp filename, which we don't want...
nlaq
Whoops, I left off the enctype. Now to mod myself down.
Jon Dowdle
A: 

Another option might be to have client-side code populate a hidden form field with the filename, which you would then have server-side.

Ben Doom's answer is generally how I would approach it, though.

Al Everett
Yeah, we considered this too. But you can't count on your users not wielding a fierce FireBug to make your life miserable. I suppose you can kick the request back and flag the user if you find that the clientside var lied (once you do the upload and get the real filename)... But I would like a more elegent solution. Thanks for the answer though :)
nlaq
A: 

The answer by Jon is correct - it does return the actual filename (not the temp one).

Try it out:

<cfif structKeyExists(form,"filePath")>
    <cfdump var="#form#">
</cfif>

<form name="whatever" action="" method="post">
    <input type="file" name="filePath">
    <input type="submit" value="ok">
</form>

When I do this, I get the original filename dumped out... If you don't, then it could be that the webserver is set to not allow the original filename to be passed through as a security measure (so people can't upload files then execute them). I don't think there is a setting for this in CF so it might be a webserver setting.

edr
+1  A: 

Here's how we do it. Basically, there is a file field, and a string field. JavaScript grabs the filename from the browser before the form is submitted. Obviously, you need to verify that the filename on the other end is actually present (it'll be blank if the user has JavaScript disabled, for example) and you'll need to parse the string to handle platform differences (/users/bob/file.jpg versus C:\Documents and Settings\bob\file.jpg)

<script>
    function WriteClientFileName(){
     $('ClientFileName').value = $('ClientFile').value;
    }
</script>

<form enctype="multipart/form-data" onsubmit="WriteClientFileName();">

    <input type="File" name="ClientFile" id="ClientFile">
    <input type="hidden" name="ClientFileName" id="ClientFileName" value="">

    <input type="submit">
</form>

Incidentally, this technique is cross-language. It'll work equally well in RoR, PHP, JSP, etc.

Edit: If a user is "wielding a fierce FireBug" what's the issue? Even if they don't have Firebug, they can still rename the file on their end and change the input. Plus, you're validating your inputs, right?

Toxf
A: 

There is no way to know the file name for uploaded files before saving to the server in ColdFuson, Railo or OpenBD. I typically generate 'my' new filename using the createUUID() function in advance of saving the file.

Aaron Greenlee
A: 

WOW, i found a great and easy solution! with a little javascript

In this way you get the temp filename for the cffile upload and the actual file.jpg name for the database

<html>
<head>
<script type="text/javascript">
function PassFileName()
{
document.getElementById("fileName").value=document.getElementById("fileUp").value;
}
</script>
</head>
<body>
<form name="form1" method="post" enctype="multipart/form-data" >
File: <input type="file" name="fileUp" id="fileUp" size="20" onchange="PassFileName()" /> <br />
Title: <input type="text" name="Title" id="Title"><br />
<input type="hidden" id="fileName" size="20" name="fileName" />
<input type="submit" name="submit">
</form>
</body>
</html>
Gustavo Obregon