cffile is overwriting existing file - coldfusion

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.

Related

ColdFusion Write File

I'm reading a simple file of logset.txt which only contains
Login:,Pass:
File is stored on webroot.
Reading it, and if no data after :. I want user to set their login data and store it.
So they would enter on a form their login and password.
So enter Bob and Turkey
And then I want it to Overwrite the logset.txt file as: Login:Bob,Pass:Turkey
I am trying this with no luck.
<cfset nf = "Login:Bob,Pass:Turkey">
<CFFILE ACTION="Write"
FILE="logset.txt"
nameConflict="overwrite"
OUTPUT="#nf#">
Is this a path issue? I have tried "/logset.txt" and "\logset.txt"
Ignore.
Path Problem This works.
<cfset fpath = expandpath('\')>
<CFFILE ACTION="Write"
FILE="#FPath#logset.txt"
nameConflict="overwrite"
OUTPUT="#nf#">
Now anyone got an idea of how to write to a mobile device local?

CFFILE creating a file with APPEND or WRITE

I'm having an issue with a coldfusion11 website, under certain conditions my attempt to create and send a report file is failing, apparently due to permission issues. I can't recreate the issue on in my test/dev environment so I need to understand what is happening for a live fix, I can't just start arbitrarily changing code on production. The current code creates the file using an APPEND action like this:
<cfset f_dir = EXCEL_PATH >
<cfset f_name = CreateUUID() & ".csv">
<cffile action="APPEND" file="#f_dir##f_name#" output="My Report "
addnewline="Yes">
<cffile action="APPEND" file="#f_dir##f_name#"
output="Title,#attributes.title#" addnewline="Yes">
Elsewhere in the code are similar functions that use WRITE as the first cffile action, like this:
<cfset f_dir = EXCEL_PATH >
<cfset f_name = CreateUUID() & ".csv">
<cffile action="WRITE" file="#f_dir##f_name#" output="My Report "
addnewline="Yes">
<cffile action="APPEND" file="#f_dir##f_name#"
output="Title,#attributes.title#" addnewline="Yes">
The code that uses WRITE first is not failing, this leads to my question:
Is there a difference between creating a file with action="APPEND" and action="WRITE" in coldfusion 11?
Ron - Write is "create" - it makes a new file. Append is used to add data to an existing file on the disk. They are different actions.
Try modifying your code as follows:
<cflock name="#f_name#">
<cfif NOT fileexists(f_dir & f_name)>
... do your WRITE action>
</cfif>
</cflock>
<cflock name="#f_name#">
.... do your APPEND action - you can be sure your file exists at this point.
</cflock>
Note, I usually use a named lock to serialize these two actions. Sometimes your code trips over file handles not quite released. Not typical but if your disk is ever thrashing it can happen.

Duplicate filenames when writing files in ColdFusion

I am trying to fix some code for a client. The issue is, we have .CSV files that are being written in ColdFusion but duplicates are constantly being generated for some reason, even with nameConflict = "Overwrite" in there. I am seeing that nameConflict may not even work with cffile action="write", how could I add maybe just an ascending number to the end of a duplicate file name every time it is generated? The CSV files simply show customer renewal information.
<cfif arguments.isRenewal EQ "Y">
<cffile action="write" file="#filename#" nameconflict="overwrite" output='"Capital","#custName#","","#getCustomer.fname#","#getCustomer.lname#","#getCustomer.serviceAddress1#","#getCustomer.serviceAddress2#","#getCustomer.serviceCity#","#getCustomer.serviceState#","#getCustomer.serviceZip#","#cleanPhone#","#cleanMobilePhone#","#getCustomer.email#","#getCustomer.billingAddress1#","#getCustomer.billingAddress2#","#getCustomer.billingCity#","#getCustomer.billingState#","#getCustomer.billingZip#","0","N/A","Electric","#getCustomer.sdi#","#getCustomer.sdi#","#getProvider.utilityCode#","#getOffer.rateSchedule#","#getOffer.productCode#","#getOffer.rate/100#","kWh","$","#getOffer.rackRateName#","#DateFormat(now(), "yyyy-mm-dd")#","#startDate#","#getOffer.term#","Internet","","","","","","","","","","","","","M2M","#getOffer.evergreenProduct#","Y"'>
<cfelse>
<cffile action="write" file="#filename#" nameconflict="overwrite" output='"Capital","#custName#","","#getCustomer.fname#","#getCustomer.lname#","#getCustomer.serviceAddress1#","#getCustomer.serviceAddress2#","#getCustomer.serviceCity#","#getCustomer.serviceState#","#getCustomer.serviceZip#","#cleanPhone#","#cleanMobilePhone#","#getCustomer.email#","#getCustomer.billingAddress1#","#getCustomer.billingAddress2#","#getCustomer.billingCity#","#getCustomer.billingState#","#getCustomer.billingZip#","0","N/A","Electric","#getCustomer.sdi#","#getCustomer.sdi#","#getProvider.utilityCode#","#getOffer.rateSchedule#","#getOffer.productCode#","#getOffer.rate/100#","kWh","$","#getOffer.rackRateName#","#DateFormat(now(), "yyyy-mm-dd")#","#startDate#","#getOffer.term#","Internet","","","","","","","","","","","","","M2M","#getOffer.evergreenProduct#","N"'>
duplicates snapshot

SpreadsheetWrite

I'm using CFScript to create and save a spreadsheet. However, SpreadsheetWrite isn't producing a file.
<cfscript>
...
...
...
spreadsheetWrite(sObj, yourSheet, "yes");
</cfscript>
It does not produce an error, but there's no file either. If I remove spreadsheetwrite from the cfscript tag and use:
<cfspreadsheet action = "write" fileName = "#yourSheet#" name = "sObj">
... I get the file just fine.
Was this not the intended use of SpreadsheetWrite()? Also, is it possible to produce a spreadsheet directly to the browser like cfdocument, or do I have to save the file first?
(Update: With relative paths it may be saving the file somewhere you are not expecting. Use an absolute path so there are no guessing games.)
This works for me using absolute paths.
<cfscript>
yourSheet = "c:/myFile.xls";
sObj = SpreadSheetNew("MySheet");
SpreadsheetAddRow(sObj,"Foo,Bar",1,1);
SpreadSheetWrite(sObj, yourSheet, true);
WriteOutput("Exists?= "& FileExists(yourSheet));
</cfscript>
To display the spreadsheet in the browser, use SpreadSheetReadBinary and cfcontent. Obviously use the appropriate mime type and file extension.
<cfheader name="Content-Disposition" value="attachment; filename=SomeFile.xls" />
<cfcontent type="application/vnd.msexcel" variable="#SpreadSheetReadBinary(yourSpreadSheetObject)#" />

How to get file extension when renaming file with cffile

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