MakeUnique not quite what I want

Posted on 2006-05-10
Last Modified: 2013-12-24
I would like to upload a file, but use my own filename instead of the user's.
Right now I have:
<cffile action="upload" filefield="FORM.ImageName" destination="#Temp#" nameconflict="MakeUnique">

But I'd like to call it my own name by doing:
<cfset Counter = Counter + 1>
And naming the file 00000001.jpg, 00000002.jpg, etc.

Q: What's the best way to do that?  Should I follow the action="upload" with an action="move"?
Question by:psenn
    LVL 12

    Expert Comment

    move would probably work, but better still would be:

    <cffile action="rename" source="path_to_existing" destination="new_path_and_name" />
    LVL 3

    Accepted Solution

    try this
    read file from server:
    <cffile action="UPLOAD" FILEFIELD=form.FileName destination="c:\temp" nameconflict="MAKEUNIQUE">

    count no of jpg in directory
    <cfdirectory action="list" directory="c:\jpg" filter=".jpg" name="filecount">

    setup file name by adding 0 the first file name is 00000000.jpg
    <cfset sourceNo = "#filecount.recordcount#">
    <cfset loopCount = 8 - #len(filecount.recordcount)#>
    <cfloop index="x" from="1" to="#loopCount#">
          <cfset sourceNo = "0" & #sourceNo#>

    rename the current file to sourceNo
    <cffile action="rename" source="c:\temp\#File.ServerFile#" destination="c:\jpg\#sourceNo#.#file.clientfileext#">
    <cfdirectory action="list" filter=".jpg" name="filecount">

    the above code should work.

    Author Comment


    Wow!  That is clever!
    I woke up this morning thinking that I was going to have to create a new table just to keep track of the counter, but your solution is much more elegant.

    However, I'm concerned about two (or more) people uploading files at the exact same time, so after giving your solution some thought, I think I've decided to fall back to the more conservative adding 1 to a counter and updating a record.

    But thanks for expanding my view.
    LVL 3

    Expert Comment

    try to put a temp file before the rename and remove after rename so you can check if the file  exists you will wait for it to finish before do the next file as the file rename only to millisecond and should not affect other user's upload.
    LVL 7

    Expert Comment

    pcaioo's suggestion is clever, but it's overly complicated.  Lose the loop to create the filename and use the NumberFormat() function again:

    <cffile action="upload" filefield="form.FileName" destination="c:\temp" nameconflict="MAKEUNIQUE">
    <cfdirectory action="list" directory="c:\jpg" filter=".jpg" name="filecount">
      newfilename = NumberFormat(Val(filecount.recordcount) + 1, "00000000");
    <cffile action="rename" source="c:\temp\#File.ServerFile#" destination="c:\jpg\#newfilename#.#file.clientfileext#">
    If you're really concerned about simultaneous uploads, then use a named lock around this bit of code.
    LVL 3

    Expert Comment

    as my code is copy from one of my old program which pass in dynamic number lengh so NumberFormat not work in my case. but I agree that NumberFormat is easier to use and understand
    LVL 7

    Expert Comment

    You can still use NumberFormat when the mask is dynamic:

    NumberFormat(Val(filecount.recordcount) + 1, RepeatString("0", number_length));
    LVL 3

    Expert Comment

    thanks never use that .

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
    One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now