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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pure ASP File Upload (can you rename file)

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
Does anyone know of a way to rename a file before saving it.
My problem is if someone uploads a file with the same name as a file already loaded.

It will overwrite it. I want to prevent this.

If you know of any suggestions I would appreciate it
 
I have not worked with Pure ASP File Upload but the file name is a parameter that is pulled from the object reference passed from the form. Somewhere in the code it uses that name to set the uploaded files name and you should be able to replace it with what you like.

Is the upload code a compiled object? If not then look through the section of code that does the writing to the server and you should see the name as a variable in that function.

Ultimately you would have to run additional code to test for the existance of the file by that name and then decide how to alter the name to make it unique, then pass that value back to the upload script.


Paranoid? ME?? WHO WANTS TO KNOW????
 
In your upload.asp file there is a function SaveToDisk
Code:
	Public Sub SaveToDisk(sPath)
		Dim oFS, oFile
		Dim nIndex
	
		If sPath = "" Or FileName = "" Then Exit Sub
		If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\"
	
		Set oFS = Server.CreateObject("Scripting.FileSystemObject")
		If Not oFS.FolderExists(sPath) Then Exit Sub
		
		Set oFile = oFS.CreateTextFile(sPath & FileName, True)
		
		For nIndex = 1 to LenB(FileData)
		    oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
		Next

		oFile.Close
	End Sub

In the bit of code above it is using the variable FileName to set the name of the file being stored. You can substitute that with anything.

My suggestion would be to write a function that checks the folder for a specific filename, pass in the FileName variable to it so it knows what to look for and have it return true or false. If it finds the file you have to then alter the filename to make it unique and test again just to make certain. Once you have a unique filename then you use that as the value to store under.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top