How to create unique file name using PureASPupload Support
Question:
Uploading files without checking for an existing file of the same name is hazardous, depending on the application.
Answer:
Generate a random string and then append original filename to it so filename now looks like {Hs8Eh-he4sD-Bx2xD}-filename.ext
Add this code somewhere near the top of your page.
<%
'generate a random string to create unique file name
randomize
SD_count = 1
DO
'get random number b/w 48 & 122 (0 - Z)
intRnd = int(75 * rnd + 48)
'limit string to 0-9, a-z, A-Z
if (intRnd < 57 OR intRnd > 65) AND (intRnd < 90 OR intRnd > 97) then
listRnd = listRnd & chr(intRnd)
SD_count = SD_count + 1
END IF
'make random string 15 chars gives 768,909,704,948,766,668,552,634,368 possible combinations on UNIX (case sensitive) or 221,073,919,720,733,357,899,776 combinations under Windows - should be enough :o)
LOOP UNTIL SD_count = 16
'make it a little more readable
strRandom = "{" & mid(listRnd,1,5) & "-" & mid(listRnd,6,5) & "-" & mid(listRnd,11,5) & "}-"
%>
Version 1: find the line that starts with GP_strm2.SaveToFile and add the strRandom variable to the string
GP_strm2.SaveToFile Trim(Server.mappath(GP_curPath))& "\" & strRandom & UploadRequest.Item(GP_curKey).Item("FileName"),2
Version 2: find the line that starts with if UploadRequest.Item(GP_curKey).Item("FileName") <> ""
and add the following lines after after it - NOTE THE TWO OPTIONS HERE
to store the original filename in the database:
'add our random filename to the object
UploadRequest.Item(GP_curKey).Item("FileName") = strRandom & UploadRequest.Item(GP_curKey).Item("FileName")
I only wanted to store the original filename in the db, but this is how to store the full unique filename in the database (thanks to Pablo Deeleman)
'add our random filename to the object
UploadRequest.Item(GP_curKey).Item("value") = strRandom & UploadRequest.Item(GP_curKey).Item("FileName")
That should do the trick...
And a BIG thanks to George Petrov for the original PureASPupload code...no wonder it is the most popular extension at UDzone.
NOTE: This works for version 1.0 and 1.5 only. In the new ver 2 there is already a feature to make filenames unique
DISCLAIMER:
This is extra complimentary content which purpose is to show additional usage that is not part of the product, i.e. it suggests tips for extending the functionality of the product by the users themselves.
It is provided "as is", without warranty of any kind, express or
implied , including but not limited to the warranties of
merchantability, fitness for a particular purpose and nonfringement of
third party rights.
DMXzone does not take responsibility to
support the suggested content to be fully compatible and working as
intended and does not provide support for extended functionality which
is not included in the current release of the extension.
It is highly recommended that only more advanced developers use it.
Comments
Combination with Server.MapPath directives
May be some of you out there will try to use this useful script in combination with other ASP code that handles with the image size (ASPimage, others, etc).
Usually, these programs make use of the Server.MapPath directive to check some instances from the file, but the symbol "{" included in the unique filename generated by this code is not compatible with the Paths supported by Server.MapPath.
In this case, you just need to modify a little the original code as follows:
Add this code somewhere near the top of your page.
<%
'generate a random string to create unique file name
randomize
SD_count = 1
DO
'get random number b/w 48 & 122 (0 - Z)
intRnd = int(75 * rnd + 48)
'limit string to 0-9, a-z, A-Z
if (intRnd < 57 OR intRnd > 65) AND (intRnd < 90 OR intRnd > 97) then
listRnd = listRnd & chr(intRnd)
SD_count = SD_count + 1
END IF
'make random string 15 chars gives 768,909,704,948,766,668,552,634,368 possible combinations on UNIX (case sensitive) or 221,073,919,720,733,357,899,776 combinations under Windows - should be enough :o)
LOOP UNTIL SD_count = 16
'make it a little more readable.
'All instances of "{" removed
strRandom = mid(listRnd,1,5) & "-" & mid(listRnd,6,5) & "-" & mid(listRnd,11,5) & "-"
%>
And that´s all, folks! I find this tip quite useful, and i recommend to save this code onto your projects as an external file (whenever SSI is supported) and use it extensively.
Happy coding!
Pablo
RE: Combination with Server.MapPath directives
Please contact me (waldo@udzone.com) when you would like to do that. I would appreciate it.
Saving Both Unique Filename to DB and In Actual File Name
Saving unique name to DB
This is a great little addon, which is very useful. Only problem is, the unique name is not being saved in my database. The original name is being saved only. The uploaded file itself is being named OK.
I tried both options listed. I have been trying to pick through the code myself, and I can't see how the code changes mentioned (in red above) will actually affect what is saved in the database. What am I missing? Any suggestions?
You must me logged in to write a comment.