coldfusioncffile

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

Solution

  • 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.