• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

MakeUnique not quite what I want

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"?
1 Solution
move would probably work, but better still would be:

<cffile action="rename" source="path_to_existing" destination="new_path_and_name" />
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.
psennAuthor Commented:

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.
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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.
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.
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
You can still use NumberFormat when the mask is dynamic:

NumberFormat(Val(filecount.recordcount) + 1, RepeatString("0", number_length));
thanks never use that .

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now