Home     Products      Support      Corporate     Sign In 
Support Knowledge Base, Article 691
Product
FileUp
Title
How do I guarantee unique file names when uploading files to the webserver?
Problem
To ensure that files are not overwritten, a unique and retrievable file name should be generated when duplicate names are encountered in the upload directory.
Solution
This article will demonstrate two solutions for uploading files to a webserver without overwriting pre-existing files with the same name. This entails:
  • Checking to see if a file name already exists on the server that matches the uploaded file.
  • Generating a unique file name for the uploaded file, if the file name if already found to exist at that path.
  • Retrieving the new name of the uploaded file, so that it may be reported back to the user.
Solution 1: Using FileUp's CreateNewFile property:Unique file names for less than 1000 uploads of the same file name

FileUp can automatically address the first two steps of this process with the CreateNewFile property. This property is set to "false" by default. Setting it to "true" will result in the generation of unique file names if the uploaded file name is found to already exist on the webserver, at the upload path. The CreateNewFile property will append a number between 0 and 999; therefore this is only effective until the 1000th upload of the same file name. For a more detailed explanation on this property and a code sample, please see the following KB article: Creating Unique Filenames

To determine the new filename after it has been assigned, string parsing functions can be used to split it out of the ServerName property, which returns the complete path of where the file was saved to the server. A complete code sample that demonstrates the use of these functions can be located at: How to determine the filename of the uploaded file after it has been saved on the server.

Solution 2: Using FileManager: Unique file names for greater than 1000 uploads of the same file name

If it is anticipated that an application requires unique file names for potentially more than 1000 uploads of the same file, then a solution will need to be written that does not use the CreateNewFile property. This solution uses the VBScript's Randomize statement and Rnd function to generate a random number, however other solutions, such as an custom incrementing number system would also be effective. (Note: Because FileUp can parse through the upload so quickly, timestamps that do not include a millisecond value may not be able to provide a unique number when two files with the same name are uploaded in the same upload request.)

  • Use FileManager to determine if the file name already exists
  • If it does, use VBScript's Randomize statement and Rnd function to generate a random number
  • Append the number to the name of the file and call FileUp's SaveAs method to save the file with the new name
  • Use FileUp's ServerName property to confirm the newly generated file name

    <%@ LANGUAGE="VBSCRIPT" %>
    <% Option Explicit %>
    <%
    '--- Declarations
    Dim oFileUp, oFM
    Dim strPath, strFormElement
    Dim y, strOName, number
    Dim strNameToCheck, strNameWOext, strExt

    '--- Instantiate the FileUp and FileManager objects
    Set oFileUp = Server.CreateObject("SoftArtisans.FileUp")
    Set oFM = CreateObject("SoftArtisans.FileManager")

    '--- Set the path property immediately so that
    '--- the upload cache destination can be controlled,
    '--- instead of reverting to the system temp directory
    strPath = "C:\7SaveHere\"
    oFileUp.Path = strPath

    For Each strFormElement In oFileUp.Form

    '--- If it's a file, IsObject() will return True
    '--- if it's a conventional form element, it will be false
    If IsObject(oFileUp.Form(strFormElement)) Then

    '--- It's a file element, so we'll see if it's empty or not
       If Not oFileUp.Form(strFormElement).IsEmpty Then

          '--- It's not empty, check if it already exists
          '--- and provide a new name if necessary
          strOName = oFileUp.Form(strFormElement).ShortFileName
          strNameToCheck = strPath & strOName

          If oFM.FileExists(strNameToCheck) Then
             '--- Generate a random number to append to the filename
             Randomize
             number = cStr(Rnd())

             '--- Separate filename from filename extension
             y = InStr(strOName,".")
             strNameWOext = Left(strOName,y-1)

             y = Len(strOName) - y
             strExt = Right(strOName,y)

             '--- Call SaveAs so a new name can be provided
             oFileUp.Form(strFormElement).SaveAs strNameWOext & "_" & _
                number & "." & strExt
          Else
             '--- Calling Save preserves original file name
             oFileUp.Form(strFormElement).Save
          End If

       Response.Write("File " & strFormElement & " | " & _
          strOName & " Saved as: " & _
          oFileUp.Form(strFormElement).ServerName & "<BR>")
       End If
    End If
    Next

   '--- Destroy objects
   Set oFileUp = Nothing
   Set oFM = Nothing
   %>

Created : 8/6/2003 1:13:09 PM (last modified : 8/6/2003 1:13:06 PM)
Rate this article!
Comments