Codice PHP:
Class UploadManager
Public TypologyField
Public TypologyFile
Private m_itemlist ' array: key1, [val1, val1, val1], key2, [val2], ...
Sub Class_Initialize
TypologyField = 0
TypologyFile = 1
End Sub
Public Function ParseRequest(r)
Dim contenttypelist, rs, requeststring, boundry, data, i, infoend, info, value, item, ii, keyexists, iv
ReDim m_itemlist(-1)
contenttypelist = Split(LCase(r.ServerVariables("HTTP_CONTENT_TYPE")), ";")
If InStr(contenttypelist(0), "multipart/form-data") = 0 Then Err.Raise 513, "UploadManager", "Form non di tipo ""multipart/form-data"""
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Fields.Append "mBin", 201, r.TotalBytes
rs.Open()
rs.AddNew()
rs("mBin").AppendChunk r.BinaryRead(r.TotalBytes)
rs.Update()
requeststring = CStr(rs("mBin"))
boundry = Split(contenttypelist(1), "=")(1)
data = Split(requeststring, boundry)
For i = 0 To UBound(data)
infoend = InStr(data(i), vbCrLf & vbCrLf) 'two sets of crlf mark the end of the information about this field; everything after that is the value
If infoend > 1 Then
info = Mid(data(i), 2, infoend - 2) 'pull the info for this field, minus the stuff at the ends...
value = Mid(data(i), infoend + 4, Len(data(i)) - infoend - 7) 'skip the crlf pairs at the start and the crlf-- at the end: length = Len(data(i)) - 4 - (infoend + 4) + 1 = Len(data(i)) - 4 - infoend - 4 + 1 = Len(data(i)) - infoend - 7
If InStr(LCase(info), "filename=") > 0 Then
Set item = New RequestItemFile
item.Info = info
item.Value = value
Else
Set item = New RequestItem
item.Info = info
item.Value = value
End If
' field exists in m_itemlist?
keyexists = False
For ii = 0 To UBound(m_itemlist) Step 2
If m_itemlist(ii) = LCase(item.Name) Then
iv = m_itemlist(ii + 1)
ReDim Preserve iv(UBound(iv) + 1)
Set iv(UBound(iv)) = item
m_itemlist(ii + 1) = iv
keyexists = True
Exit For
End If
Next
If Not keyexists Then
ReDim Preserve m_itemlist(UBound(m_itemlist) + 2)
m_itemlist(UBound(m_itemlist) - 1) = LCase(item.Name)
m_itemlist(UBound(m_itemlist)) = Array(item)
End If
End If
Next
End Function
Public Function GetItem(name) ' return first field
Dim i
Set GetItem = Nothing
For i = 0 To UBound(m_itemlist) Step 2
If m_itemlist(i) = LCase(name) Then
Set GetItem = m_itemlist(i + 1)(0)
Exit Function
End If
Next
End Function
Public Function GetItemList(name)
Dim i
GetItemList = Array()
For i = 0 To UBound(m_itemlist) Step 2
If m_itemlist(i) = LCase(name) Then
GetItemList = m_itemlist(i + 1)
Exit Function
End If
Next
End Function
End Class
Class RequestItem
Public Info
Public Value
Public Property Get Name
Dim s, e
s = InStr(Info, "name=""") + Len("name=""")
If InStrRev(Info, """;") > 0 Then
e = InStrRev(Info, """;") - 1
Else
e = InStrRev(Info, """") - 1
End If
Name = Mid(Info, s, e - s + 1)
End Property
Public Property Get Typology
Dim um
Set um = New UploadManager
Typology = um.TypologyField
Set um = Nothing
End Property
End Class
Class RequestItemFile
Public Info
Public Value
Public Property Get Name
Dim s, e
s = InStr(Info, "name=""") + Len("name=""")
If InStrRev(Info, """;") > 0 Then
e = InStrRev(Info, """;") - 1
Else
e = InStrRev(Info, """") - 1
End If
Name = Mid(Info, s, e - s + 1)
End Property
Public Property Get Typology
Dim um
Set um = New UploadManager
Typology = um.TypologyFile
Set um = Nothing
End Property
Public Property Get FullPath
Dim s, e
s = InStr(Info, "filename=""") + Len("filename=""")
e = InStrRev(Info, """" & vbCrLf) - 1
FullPath = Replace(Mid(Info, s, e - s + 1), "/", "\")
End Property
Public Property Get FileName
Dim s
s = InStrRev(FullPath, "\") + 1
FileName = Mid(FullPath, s, Len(FullPath) - s + 1)
End Property
Public Property Get FileExtension
Dim s
s = InStrRev(FileName, ".") + 1
FileExtension = Mid(FileName, s, Len(FileName) - s + 1)
End Property
Public Property Get FileSize
FileSize = Len(Value) ' byte
End Property
Public Property Get ContentType
Dim s
s = InStr(Info, "Content-Type: ") + Len("Content-Type: ")
ContentType = Mid(Info, s, Len(Info) - s + 1)
End Property
Public Function Save(remotepath, remotefilename)
Dim fn, fso, rpl, cp, i, f
remotepath = Replace(remotepath, "/", "\")
If Right(remotepath, 1) <> "\" Then remotepath = remotepath & "\"
fn = remotepath & remotefilename
Set fso = Server.CreateObject("Scripting.FileSystemObject")
' build remote path
If Not fso.FolderExists(remotepath) Then
rpl = Split(remotepath, "\")
cp = rpl(0)
For i = 1 To UBound(rml)
cp = cp & "\" & rpl(i)
If Not fso.FolderExists(cp) Then fso.CreateFolder(cp)
Next
End If
' save file
Set f = fso.OpenTextFile(fn, 2, True)
f.Write Value
f.Close
Set f = Nothing
Set fso = Nothing
End Function
End Class