Search code examples
file-uploadcoldfusionuploadcfml

Can file names be changed on uploading with cffile?


I am currently trying to create a page where bands can upload their own logos onto the site for use where it is needed. Currently, I have created something which allows a user to upload/delete a logo to the allocated directory. But what I want to do is rather than create a band_logo field, have it so the band logo file name becomes the band's id in the database. With this being unique it means I don't have the long-winded process of creating a field to save their logo name. I know there is a cffile action="rename" option but that is a more long-winded process of doing things.


Solution

  • Yes, if you provide the file name in the destination it will rename the file on upload.

    <cffile action="upload" destination="/path/to/some/directory/#session.bandName#.jpg" ... />
    

    See Renaming Files As They Are Uploaded (how CFFILE actually works)

    BTW, searching google for "name file on cffile upload" found that article...

    However, you still may want to use

    <cffile action = "upload" ...>
     then 
    <cffile action = "rename" ...>
    

    Because unless you snag the file extension on the client side and pass it on to the server, hard coding the file extension could cause problems if they don't upload a .jpg or whatever extension you designate. It isn't that much code or overhead and it's safer.