How to get file extension when renaming file with cffile - coldfusion

I'm using a cffile tag to upload my file and resave it with a new name. My issue is that the file could be a few different formats and I don't know how to detect the file extension. I'm using the code below:
<cfset ui = createUUID()>
<cffile
action="upload"
accept="video/x-flv, video/mp4, video/x-msvideo"
destination="e:\www2\uploads\#ui#.#cffile.ServerFileExt#"
nameconflict="makeunique"
filefield="form.file"
>
It's telling me that cffile is undefined.

I recommend uploading first, then renaming:
<cfset ui = createUUID()>
<cffile
action="upload"
accept="video/x-flv, video/mp4, video/x-msvideo"
destination="e:\www2\uploads\"
nameconflict="makeunique"
filefield="form.file"
/>
<cffile
action="rename"
source="e:\www2\uploads\#cffile.serverFileName#"
destination="e:\www2\uploads\#ui#.#cffile.serverFileExt#"
/>

I found this awesome function created by Ryan Stille
It should do everything you need
I used it to get the extension then I just created a file name with a UUID
<cffile action="upload" destination="file://upload_#createUUID()#.#fileExt#" nameconflict="makeunique" result="#formField#">

Related

cffile is overwriting existing file

The nameconflict attribute doesn't seem well documented, so maybe I'm using it wrong. But if I write a file (binary data) using the following code, it's overwriting the existing file of the same name.
<cffile action="write" file="/path/#data.name#" output="#d#" nameconflict="MakeUnique" mode="775">
nameconflict="MakeUnique" only works for <cffile action="upload".
Overwriting the file is default behavior for <cffile action="write"
Unless exact file names are not needed for display purpose it is better approach to use a unique ID as the filename to make sure we do not overwrite files.
<cfset newFile = '/path/' & CreateUUID() & '.' & ListLast(data.name, '.')>
<cffile action="write" file="#newFile#" output="#d#" mode="775">
Note: For use cases where we need to have a custom file name for view purposes, its better to save that original file name in the database and keep the filename in the filesystem in separate column.

How to access the object created by a cffile upload in coldfusion

I have an assortment of issues, but I'm going to concentrate on one here. How to I access the object created from a cffile upload. I am currently doing as so.
<cffile action="upload" destination="#Application.filePath#Pics/" filefield="image1" nameconflict="makeunique">
<cfif isDefined ("cffile.serverFile")>
<cfset image1Place = #cffile.serverFile#>
</cfif>
but that doesn't seem like it would work well with multiple file uploads, which happens to be my case.
If you're worried about the result object being blown away as a consequence of multiple invocations of cffile, then you can use the "result" attribute to distinguish them:
<cfset uploadResults = {}>
<cfloop list="#form.filelist#" index="myFile">
<cffile action="upload" destination="#Application.filePath#Pics/"
filefield="#myFile#" nameconflict="makeunique"
result='uploadResults.#myFile#'>
<cfif StructKeyExists(uploadResults, myFile)>
<cfset image1Place = uploadResults[myFile].serverFile>
</cfif>
</cfloop>

Saving content to a .DOC file using ColdFusion

I'm not having any issues writing the content to the .doc file. The problem I'm having is getting the file to NOT download to the user's browser automatically after creation. I just want to have the .doc file created in the background, then the user can download the file from a web page at anytime. Here's the code I'm working with:
<cfheader name="Content-disposition" value="filename=Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc">
<cfcontent type="application/msword">
<cfoutput>#WordDoc#</cfoutput>
<cffile action="copy" source="#application.AbsPath#\media\quotes\BlankQuote.doc" destination="#application.AbsPath#\media\quotes\Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc" />
<cffile action="write" file="#application.AbsPath#\media\quotes\Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc" output="#WordDoc#" />
You're problem is the <cfheader> tag... that's what is triggering the doc to open.
I would do something like this instead.
<cfsavecontent variable="whatever">
<cfoutput>#WordDoc#</cfoutput>
</cfsavecontent>
<cffile action="write" file="#application.AbsPath#\media\quotes\Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc" output="#whatever#" />

Coldfusion - cannot access templ folder when creating thumb

I am getting a problem with the following line:
<cfimage action="read" name="myImage" source="#ExpandPath("../../banner/#upload.clientfile#")#" />
I suspect it is because I am using a shared host (CF9) and do not have access to the folder. The error I get is "unable to create temporary file". My temp directory is home/kloxo/temp/wwwroot-tmp. Can I specify another temp folder or do I have to get my hosting company to sort this?
<cfapplication sessionmanagement="true">
<cfoutput>#GetTempDirectory()#</cfoutput>
<cfif IsDefined ("FORM")>
<cfif structKeyExists(form, "uploadfile")>
<cfset destination = expandPath("../../banner")>
<cfif not directoryExists(destination)>
<cfdirectory action="create" directory="#destination#">
</cfif>
<cffile action="upload" filefield="uploadfile" destination="#destination#" nameConflict="makeUnique" result="upload">
<cfdump var="#upload.clientfile#">
<cfimage action="read" name="myImage" source="#ExpandPath("../../banner/#upload.clientfile#")#" />
</cfif>
It appears to be a configuration issue. Contact your host. Here is a page with more information:
http://forums.adobe.com/message/3060530

cffile not working at all

cffile is giving a head ache now.
My cfm is like this -
`
<cfif session.ismac and session.browsermake eq "firefox">
<cfset size = "55">
</cfif>
<cfset onChange = "document.frmMain.submit1.disabled = true;setdisplayname(this,this.form.dummy);">
<cfif displayname EQ "">
<cfset size = "document.frmMain.submit1.disabled = true;setdisplayname(this,this.form.displayname);">
</cfif>
<cfinput type="file" name="File#thisUploader#" id="File#thisUploader#" size="#size#" onKeyPress="return false;" onchange="#onChange#">
`
and in my cfc the code is like this -
<cffile accept="image/*" action="upload" destination="#application.artworkfilepath#\bulkuploads\#session.loginname#\#form.category#\" filefield="form.File#thisUploader#" nameconflict="makeunique">
and if I dump - <cfoutput>
You uploaded #cffile.ClientFileName#.#cffile.ClientFileExt#
successfully to #cffile.ServerDirectory#.
</cfoutput>
<cfabort>
I get corrct things and no error.
But when i look into the folder there is nothing.
Anyidea? I have added the dump of cffile now. What do you make out of it?
cfform code is like this <cfform id="frmMain" name="frmMain" action="process_multi.cfm" enctype="multipart/form-data" target="_self" method="post">
do a fileExists() directly after the statement and let us know what that says...
you don't have a directorywatcher on the directory do you?
Your cffile nameconfict attribute is set to makeunique, which tells ColdFusion to rename the file to something new when it arrives at the server--if the file already exists.
However, you are using cffile.ClientFileName and cffile.ClientFileExt to refer to the file file--which maps to the unchanged file name as it was received during upload.
Change your code references to cffile.ServerFileName and cffile.ServerFileExt for the final renamed result.