• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Regarding arrays and SAfileUp

I am using SAfileUp with ASP to upload files to a folder. I want to prevent users from uploading files that end with web extensions (htm, html, asp, shtml, cfm, cfml) and/or force them to only upload MS Office and image files (doc, xls, ppt, gif, jpg, jpeg). I am able to get the extension of the uploaded file via this formula:

ext = Mid(upl.UserFilename, InstrRev(upl.UserFilename, ".") + 1)

And I have a table in the db that lists both acceptable (tblGood_Ext) and unacceptable extensions (tblBad_Ext). But I'm not that good at arrays to write a function for this. Please help.
0
teelions
Asked:
teelions
  • 4
  • 3
1 Solution
 
sybeCommented:
so what do you want: allow only extension that you allow, or forbid only extensions that you forbid ??

<%

aValidExtensions = Array("doc","xls","ppt","gif",jpg","jpeg")

ext = Mid(upl.UserFilename, InstrRev(upl.UserFilename, ".") + 1)

If IsElementOfArray()ext,aValidExtensions) Then
    ' file has an "allowed" extension and can be saved
Else
    ' file has no "allowed" extension, don't dave it
End If


Function IsElementOfArray(var,arr)
    Dim element
    IsElementOfArray = False
    If Not IsArray(arr) Then Exit Function

    If IsArray(arr) Then
        For Each element In arr
            If var = element Then
                IsElementOfArray = True
                Exit For
            End If
        Next
    End If
End Function
%>
0
 
sybeCommented:
typo, it should be:

If IsElementOfArray(ext,aValidExtensions) Then
0
 
teelionsAuthor Commented:
I'll try #1 and let you know if it works for me. I think it's better to check for the "allowed" extensions because then it would place a limit on what can be uploaded. Thanks.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
teelionsAuthor Commented:
I put this code in place:
  <%
'Grab document extension & check for acceptable file types
Dim aValidExtensions
Dim ext

aValidExtensions = Array("doc","xls","ppt","gif","jpg","jpeg","pdf","rtf","txt")

ext = Mid(upl.UserFilename, InstrRev(upl.UserFilename,".") + 1)

If IsElementOf(ext,aValidExtensions) Then
  Response.Write("The file type, " & ext & " is accepted.")
  Response.End
Else
  Response.Write("The file type, " & ext & " is <b>NOT</b> acceptable.")
  Response.End
End If
%>
But got this error:
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'IsElementOf'

0
 
teelionsAuthor Commented:
Sorry - This is what I placed. Got the same error though:
<%
Function IsElementOfArray(var,arr)
   Dim element
   Dim aValidExtensions
   Dim ext
   
   IsElementOfArray = False
   If Not IsArray(arr) Then Exit Function

   If IsArray(arr) Then
       For Each element In arr
           If var = element Then
               IsElementOfArray = True
               Exit For
           End If
       Next
   End If
End Function

'Grab document extension & check for acceptable file types
Dim aValidExtensions
aValidExtensions = Array("doc","xls","ppt","gif","jpg","jpeg","pdf","rtf","txt")

ext = Mid(upl.UserFilename, InstrRev(upl.UserFilename,".") + 1)

If IsElementOf(ext,aValidExtensions) Then
  Response.Write("The file type, " & ext & " is accepted.")
  Response.End
Else
  Response.Write("The file type, " & ext & " is <b>NOT</b> acceptable.")
  Response.End
End If
%>
0
 
sybeCommented:
Replace this

If IsElementOf(ext,aValidExtensions) Then

with

If IsElementOfArray(ext,aValidExtensions) Then
0
 
teelionsAuthor Commented:
It worked. Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now