2

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">
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Phaelax z
  • 1,814
  • 1
  • 7
  • 19

1 Answers1

4

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.

rrk
  • 15,677
  • 4
  • 29
  • 45
  • 2
    IMO, still a good idea even if you do need the original name. Save the original name in the database too, and use it with cfheader when downloading, etc – SOS Sep 26 '20 at 19:29
  • 1
    I probably will store the original filename, at least to have it displayed that way to the user. – Phaelax z Sep 27 '20 at 15:16