Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to prevent files from being overwritten

Status
Not open for further replies.

771212

Programmer
Oct 1, 2002
7
0
0
US
I am looking for a way to allow my users to upload files into a directory and prevent them from overwriting the file if the file name already exists.

For instance, UserA wants to upload a file name Project1. There is already a file named Project1 in there. I need to kick the user back out and let them know that they have to rename the file and try to upload the file again.

I can't use the MakeUnique - NameConflict attribute because we need to make the file name something that would make sense to the user who wishes to download the file. A name such as "Project1" would be meaningful. Each user has a unique username. Is there possibly a way to append the username to the filename if the filename alreadys exists in the directory? i.e. Project1_mike2277

We tried to use this code but no luck on ColdFusion Server MX. It works alright on 5.0 though.

<CFFILE
DESTINATION=&quot;F:\mySite\file_transfer\134
ACTION=&quot;UPLOAD&quot;
NAMECONFLICT=&quot;skip&quot;
FILEFIELD=&quot;FileName&quot;>
</cfif>

<CFIF #File.FileExisted# IS &quot;YES&quot;>
Sorry, please go back and rename your file and try to upload it again.
<CFELSE>
Congratulations! Your file has been uploaded.
</CFIF>

Our error message though on MX is:

Element FILEEXISTED is undefined in FILE.

Any suggestions would be appreciated!


 
I tried the above code on our CFMX server and it worked fine. we are running win2k, iis5 and CF MX SP2, if that is of any help!

I see from the documentation that MM now recommends using CFFile instead of File so your line <CFIF #File.FileExisted# IS &quot;YES&quot;> would change to <CFIF #CFFile.FileExisted# IS &quot;YES&quot;>

not a biggie but it might help you!

about renaming the file, you could do something like this:

<CFFILE
DESTINATION=&quot;F:\mySite\file_transfer\134
ACTION=&quot;UPLOAD&quot;
NAMECONFLICT=&quot;MAKEUNIQUE&quot;
FILEFIELD=&quot;FileName&quot;>

<CFIF File.FileExisted IS &quot;YES&quot;>
<CFSET FileLoc = CFFILE.serverDirectory & &quot;\&quot; & CFFIle.serverFileName & #Your_username_variable# & &quot;.&quot; & CFFile.serverFileExt>
<CFFILE ACTION=&quot;RENAME&quot; SOURCE=&quot;#CFFILE.serverDirectory#\#CFFile.ServerFile#&quot; DESTINATION=&quot;#variables.FileLoc#&quot;>
Congratulations! Your file has been uploaded.
<CFELSE>
Congratulations! Your file has been uploaded.
</CFIF>

hope this helps!

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top