[thelist] Persits.Upload.1 and Special Characters in the Uploaded Filename

Rob Smith rob.smith at lexjet.com
Wed Oct 18 10:29:49 CDT 2006


Hey,

 

I'm trying to fine tune some upload script from the
server.createobject("Persits.Upload.1") component supported by GoDaddy.
Here's the relevant code:

 

<% 

                Set Upload = Server.CreateObject ("Persits.Upload.1")

    Upload.SaveVirtual ("/uploads") 'save and upload file

                

                Server.ScriptTimeOut = 10000 

                

                Path = server.MapPath("/uploads")

                

                Set File = Upload.Files(1)

 

                if File.size < 2048000 then

                  ' Is this a valid image file?

                                If File.ImageType <> "UNKNOWN" Then

                                

                                                ' Create instance of
AspJpeg object

                                                Set jpeg =
Server.CreateObject("Persits.Jpeg")

                                

                                                ' Open uploaded file

                                                jpeg.Open( File.Path )

                                

                                                'reduce uploaded file to
800 max pixels wide/tall                              

                                                If jpeg.OriginalHeight <
jpeg.OriginalWidth Then

 
jpeg.Width = 800

 
jpeg.Height = jpeg.OriginalHeight * jpeg.Width / jpeg.OriginalWidth

                                                Else

 
jpeg.Height = 800

 
jpeg.Width = jpeg.OriginalWidth * jpeg.Height / jpeg.OriginalHeight

                                                End If

                                                

                                                'get new path

                                                SavePath = Path & "\" &
File.ExtractFileName

                                                

                                                'save image

                                                'response.write SavePath

                                                jpeg.Save SavePath

                                                

                                                'reset SavePath

                                                SavePath = ""

                                                

                                                'now reduce the image to
a thumbnail

                                                If jpeg.OriginalHeight <
jpeg.OriginalWidth Then

 
jpeg.Width = 100

 
jpeg.Height = jpeg.OriginalHeight * jpeg.Width / jpeg.OriginalWidth

                                                Else

 
jpeg.Height = 100

 
jpeg.Width = jpeg.OriginalWidth * jpeg.Height / jpeg.OriginalHeight

                                                End If

                                

                                                

                                                SavePath = Path &
"\small_" & File.ExtractFileName

                                                

                                                

                                                vid = session("vid")

                                                title =
Upload.form("title")

                                                thumbfilename =
"uploads/small_" & File.ExtractFileName

                                                filename = "uploads/" &
File.ExtractFileName

                                                

                                                filesize = File.Size

                                                

                                                if filesize >= 1024 and
filesize < 1048576 then

                                                                filesize
= round(filesize/1024,1) & " Kb"

                                                elseif filesize >=
1048576 then

                                                                filesize
= round(filesize/1024/1024,1) & " Mb"

                                                else 

                                                                filesize
= filesize & " bytes"

                                                end if

                                                

                                                dateadded =
datepart("yyyy",date()) & "-" & datepart("m",date()) & "-" &
datepart("d",date()) & " " & datepart("h",date()) & ":" &
datepart("n",date()) & ":" & datepart("s",date())

 


                                                If (not
UCase(Right(trim(SavePath), 3)) = "JPG" and not
UCase(Right(trim(SavePath), 3)) = "GIF")  Then

                                                  SavePath = SavePath &
".jpg"

                                                End If

                                                

                                                If (not
UCase(Right(trim(thumbfilename), 3)) = "JPG" and not
UCase(Right(trim(thumbfilename), 3)) <> "GIF")  Then

                                                  thumbfilename =
thumbfilename & ".jpg"

                                                End If

                                

                                                jpeg.Save SavePath

                                

                                                set jpeg = nothing

                                                   

                                                response.redirect
"InsertSQLPhoto.asp?result=Good&title=" &
replace(replace(title,"'","''"),"&","and") & "&vid=" & vid & "&path=" &
filename & "&thumbpath=" & thumbfilename & "&filesize=" & filesize &
"&dateadded=" & dateadded

                  Else

                                                Response.Write "This is
not a valid image."

                                                Response.End

                                                response.redirect
"MyStuff.asp?result=Bad"

                  End If

   else

                                response.redirect
"MyStuff.asp?result=toolarge"

   end if

 

%>

 

When a user has an & (ampersand) or other special characters in the file
name things start acting funny. Is there a way I can catch the filename
just before it's uploaded and rename it? Or is something I need to
address after it's uploaded and resave the file with a new filename,
then update the databases, file management, paths, etc.

 

Thanks


Rob Smith
LexJet
rob.smith at lexjet.com
http://www.lexjet.com
(800)453-9538
(941)330-1210 Int'l
(941)330-1220 Fax
1680 Fruitville Road, 3rd Floor
Sarasota, FL 34236




More information about the thelist mailing list