Combination with Server.MapPath directives

July 31, 2001 by Pablo Deeleman

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

July 31, 2001 by Waldo Smeets
Cool Pablo! Thank you very much. Maybe you can turn this FAQ and your post into a nice tutorial? It is more than a FAQ right now ;)
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

October 24, 2001 by Michael Youngblood
This is generally wonderful, but I want to save a unique file name to the database as well as modify the actual file name when it gets uploaded into my 'uploads' directory.  Using these two options, I seem to get one or the other.  I must be missing something.  THANKS!

Saving unique name to DB

January 18, 2002 by Mike Hyde

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?

See all 9 Comments