'******************************************************************
' Checkfiletype function to check whether a file is a picture file
' parameter filename is the path to the local file
' If it's one of the file jpeg,gif,bmp,png pictures, the function returns True, otherwise it returns false
'******************************************************************
Function checkfiletype (filename)
Const Adtypebinary=1
Dim jpg (1): JPG (0) =cbyte (&HFF): JPG (1) =cbyte (&HD8)
Dim bmp (1): BMP (0) =cbyte (&h42): BMP (1) =cbyte (&H4D)
Dim png (3):p ng (0) =cbyte (&h89):p ng (1) =cbyte (&h50):p ng (2) =cbyte
(&h4e):p ng (3) =cbyte (&H47)
Dim gif (5): GIF (0) =cbyte (&h47): GIF (1) =cbyte (&h49): GIF (2) =cbyte
(&h46): GIF (3) =cbyte (&h39): GIF (4) =cbyte (&h38): GIF (5) =cbyte (&h61)
On Error Resume Next
Checkfiletype=false
Dim Fstream,fileext,stamp,i,fseteam
Fileext = Mid (Filename,instrrev (FileName, ".") +1)
Set fstream = Server.CreateObject ("ADODB. Stream ")
FStream. Open
FStream. Type=adtypebinary
FStream. LoadFromFile filename
Fstream.position=0
Select Case Fileext
Case "JPG", "JPEG"
Stamp=fstream.read (2)
For i=0 to 1
If AscB (MidB (stamp,i+1,1)) =jpg (i) then checkfiletype=true else
Checkfiletype=false
Next
Case "GIF"
Stamp=fstream.read (6)
For I=0 to 5
If AscB (MidB (stamp,i+1,1)) =gif (i) then checkfiletype=true else
Checkfiletype=false
Next
Case "PNG"
Stamp=fstream.read (4)
For I=0 to 3
If AscB (MidB (stamp,i+1,1)) =png (i) then checkfiletype=true else
Checkfiletype=false
Next
Case "BMP"
Stamp=fstream.read (2)
For i=0 to 1
If AscB (MidB (stamp,i+1,1)) =bmp (i) then checkfiletype=true else
Checkfiletype=false
Next
End Select
FStream. Close
Set fseteam=nothing
If err.number<>0 then Checkfiletype=false
End Function
Site original. NET Tutorial > Www.111cn.net