Renaming a File

K

Keith

Is it possible to rename a file which a user uploads to the server?

I have a form which allows users to upload an image to a folder on the web
server. This works fine.

The form they submit takes their firstname, surname and the file.

However, I want the file to automatically be renamed to
firstname_surname.jpg (taking their firtname and surname from the form they
are submitting).

Is this possible?
 
J

John

I use a the FREE upload component of www.dundas.com with full HELP on-line.


Hereunder the ASP that rename the uploaded files to a random number

<%
Session("sender")="upload.asp"
If Session("userId")="" then Response.redirect "login.asp"

Response.Write "<html>"
Response.Write "<header>"
Response.Write "<title>Fotoserver HSW-MVA Upload</title>"
%>
<!--#Include file="common/conStr.inc"-->
<%
Response.Write "<link rel=""stylesheet"" type=""text/css""
href=""common/css.inc"">"
Response.Write "</header>"
Response.Write "<body>"

'On Error Resume Next

Set MyConn=Server.CreateObject("ADODB.Connection")

MyConn.Open Connect_String

imagesPath = "upload"
theDate = Right("00" & Month(date()),2) & "/" & Right("00" & Day(Date()),2)
& "/" & Year(Date())

Set objUpload = Server.CreateObject ("Dundas.Upload.2")

objUpload.SaveToMemory

fileName = Trim(objUpload.Form("ref"))
credits = Ucase(Trim(objUpload.Form("credits")))
fileNr = 0

if fileName = "" then
Response.Write "Ongeldige referentie."
Else

if objUpload.DirectoryExists(Server.MapPath(imagesPath & "\" &
Session("userId") & "\" & fileName)) then
objUpload.DirectoryDelete Server.MapPath(imagesPath & "\" &
Session("userId") & "\" & fileName),1
End if

objUpload.DirectoryCreate Server.MapPath(imagesPath & "\" &
Session("userId") & "\" & fileName)

If Err.Number <> 0 Then

Response.Write "Error " & Err.Number

Else

Response.Write "Aanbieder: " & Session("userCompany") & " / " &
Session("userId") & "<br><br>"

Response.Write "<table width=""760"" class=""small"" style=""border:#ffffff
1 solid border"">"
Response.Write "<tr><td width=""600"">Uploaded files</td><td
width=""160"">Saved as</td></tr>"

For Each objUploadedFile in objUpload.Files

if Ucase(Right(objUploadedFile.OriginalPath,4))=".JPG" Or
Ucase(Right(objUploadedFile.OriginalPath,4))=".GIF" then
extention = Ucase(Right(objUploadedFile.OriginalPath,4))
fileNr = fileNr + 1

randomize
nr1 = Clng(Rnd * 1000000)+1
if nr1 > 1000000 then nr1 = 1000000
if nr1 < 1 then nr = 1

randomize
nr2 = Clng(Rnd * 1000000)+1
if nr2 > 1000000 then nr2 = 1000000
if nr2 < 1 then nr = 1

randomize
nr3 = Clng(Rnd * 1000000)+1
if nr3 > 1000000 then nr3 = 1000000
if nr3 < 1 then nr = 1

Response.Write "<tr><td width=""660"">" & objUploadedFile.OriginalPath &
"</td><td width=""100"">" & Cstr(Nr1) & Cstr(Nr2) & Cstr(Nr3) & extention &
"</td></tr>"

'save to server
objUploadedFile.SaveAs Server.MapPath(imagesPath & "/" & Session("userId")
& "/" & fileName & "/" & Cstr(Nr1) & Cstr(Nr2) & Cstr(Nr3) & extention)
End If
Next

Response.Write "</table>"


Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile(Server.MapPath(imagesPath & "/" &
Session("userId") & "/" & fileName & "/default.asp"), True)
tf.WriteLine("Oops")
tf.Close



'Update dbs (tblUploads)

MyConn.Execute "DELETE * FROM tblUploads WHERE uploadId=" & _
Chr(34) & Session("UserId") & Chr(34) & " AND upLoadRef=" & _
Chr(34) & fileName & Chr(34)


sql="INSERT INTO tblUploads
(uploadId,uploadRef,uploadX,uploadDate,uploadTime) VALUES (" & _
Chr(34) & Session("UserId") & Chr(34) & "," & _
Chr(34) & fileName & Chr(34) & "," & _
Cint(fileNr) & "," & _
"#" & theDate & "#," & _
"#" & time() & "#" & ")"

MyConn.Execute sql
Set MyConn=Nothing

End If

End if 'bad filename

Set objUpload = Nothing
Response.Write "</body></html>"

'Response.Write "<a href=""upload.asp"">Volgende</a>"
'Response.Redirect "upload.asp"



%>
 
A

Aaron [SQL Server MVP]

Is it possible to rename a file which a user uploads to the server?

Yes. Depending on the component you use for upload, you can do this during
the upload (e.g. component.SaveAs(somefilename)) or use FileSystemObject
after it's been uploaded. For the former, see the documentation for your
upload component. For the latter, see:

http://www.aspfaq.com/2074
http://www.aspfaq.com/2039#rename
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top