Her er det, men det lægger det direkte i en mappe på din server:
html><body>
<?
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))
{
umask(033);
copy($HTTP_POST_FILES['userfile']['tmp_name'], "path/".$HTTP_POST_FILES['userfile']['name']);
// Bemærk / efter path navnet. Den skal med.
// Windows brugere, erstat "path" med "d:\\home\\FtpBrugernavn\\db\\"
}
else
{
echo "Possible file upload attack: filename ", $HTTP_POST_FILES['userfile']['name'];
}
?>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.php" METHOD="POST">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="2000000">
Send this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
</body></html>
At gemme billeder el.lign. i en database, uanset hvilken, er noget af det værste du kan gøre performancemæssigt. Du bør i stedet uploade billederne til filsystemet (som i eksemplet) og blot gemme billedets sti/navn i databasen.
Nu er de 2 sek. vist gået og du har endnu ikke fået ASP versionen :-) Hvis jeg husker det, kigger jeg forbi i morgen og hvis du ikke har fået scriptet, skal jeg nok finde noget frem.
Her er side 1 til upload uden komponenter siden skal hedde upload.asp:
<%@ Language="VBScript" %>
<HTML>
<head>
<!--#include file="fileupload.inc" -->
</head>
<body>
<%
If Request.ServerVariables("REQUEST_METHOD") <> "POST" Then
%>
<h3>Upload fil</h3>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.asp" METHOD="post">
<P>Vælg en fil på max 10Mb, som du ønsker at uploade.<BR>
Sti: <INPUT NAME="fileupload" TYPE="file" class="knap"><BR><BR>
<INPUT NAME="Action" TYPE="submit" VALUE="Upload" class="knap">
</FORM>
<%
Else
Dim intFileUpload
Dim strPath, intMaxSize, arrAcceptType(1), arrAcceptExt(1), strContentType, strFilename, intFileTotalBytes, strNewFilename
StrPath = "../billeder/" 'Stien hvor billedet skal placeres
intMaxSize=10000000
arrAcceptType(0)=""
'arrAcceptType(0)="image/gif"
'arrAcceptType(1)="image/jpeg"
'arrAcceptType(2)="image/pjpeg"
'arrAcceptType(3)="application/octet-stream"
'arrAcceptType(4)="text/html"
'arrAcceptType(5)="text/plain"
arrAcceptExt(0)=""
'arrAcceptExt(0)="gif"
'arrAcceptExt(1)="jpg"
'arrAcceptExt(2)="jif"
'arrAcceptExt(3)="jpeg"
'arrAcceptExt(4)="xls"
'arrAcceptExt(5)="doc"
'arrAcceptExt(6)="html"
'arrAcceptExt(7)="htm"
'arrAcceptExt(8)="asp"
'arrAcceptExt(9)="txt"
'arrAcceptExt(10)="bin"
'arrAcceptExt(11)="mp3"
intFileUpload = FileUpload(strPath, intMaxSize, arrAcceptType, arrAcceptExt, strContentType, strFilename, intFileTotalBytes, "")
If intFileUpload = 0 Then
Response.Write "Filen " & strFilename & " blev uploaded.<BR>"
Response.Write "Det er en fil af typen " & strContentType & " og den fylder " & intFileTotalBytes & " bytes:<BR>"
Else
Response.Write "Der opstod en fejl under upload!<BR>"
End If
End If
%>
</BODY>
</HTML>
Denne fil ligges i samme mappe som upload.asp:
<%
dim encUd, c
function encodePakke(encInd)
encUd = ""
for c = 1 to lenb(encInd)
encUd = encUd & Chr(AscB(MidB(encInd, c, 1)))
next
encodePakke = encUd
end function
Function FileUpload(strPath, intMaxSize, arrAcceptType, arrAcceptExt, ByRef strContentType, ByRef strFilename, ByRef intFileTotalBytes, ByRef strNewFilename)
Dim intPostTotalBytes, intStartPos, intEndPos, i
Dim bstrPostData, bstrDivider
Dim strTemp, strFileSpec
Dim arrSplit
Dim vbCrLfB
Dim bolStopLoop, bolContentTypeOK, bolExtOK
Dim fs, ts, f
strContentType = ""
strFilename = ""
intFileTotalBytes = 0
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
vbCrLfB = ChrB(13) & ChrB(10)
intPostTotalBytes = Request.TotalBytes
bstrPostData = Request.BinaryRead(intPostTotalBytes)
If LenB(bstrPostData) <> intPostTotalBytes Then 'Check: Er antallet af bytes i POST forskelligt fra den binære streng?
FileUpload = 1
Exit Function
End If
bstrDivider = LeftB(bstrPostData, InStrB(bstrPostData, vbCrLfB) + 1)
intStartPos = 1
bolStopLoop = False
Do
intStartPos = InStrB(intStartPos, bstrPostData, bstrDivider) + LenB(bstrDivider)
If intStartPos = 0 Then
FileUpload = 2
Exit Function
End If
intEndPos = InStrB(intStartPos, bstrPostData, bstrDivider)
If intEndPos = 0 Then
FileUpload = 2
Exit Function
End If
strTemp = bin2str(MidB(bstrPostData, intStartPos, InStrB(intStartPos, bstrPostData, vbCrLfB) - intStartPos))
If InStr(LCase(strTemp), "name=""fileupload""") > 0 Then
bolStopLoop = True
Else
intStartPos = intEndPos
End If
Loop Until bolStopLoop
intStartPos = intStartPos + Len(strTemp) + 2
arrSplit = Split(strTemp, ";") 'Opdel strTemp ved ;: Content-Disposition: form-data; name="fileupload"; filename="filen.txt"
strTemp = "" 'Værdi ved fejl
For i = 0 To UBound(arrSplit) 'Køres for alle i denne array
If LCase(Left(Trim(arrSplit(i)), 9)) = "filename=" Then 'Står der filename= ?
strTemp = Trim(arrSplit(i))
Exit For
End If
Next
If strTemp = "" Or strTemp = "filename=""""" Then
FileUpload = 3
Exit Function
End If
arrSplit = Split(strTemp, """")
strTemp = arrSplit(UBound(arrSplit) - 1) 'Næstsidste indholder filnavn
arrSplit = Split(strTemp, "\") 'Del ved alle \ Så indeholder den sidste filnavn.ext"
strFilename = arrSplit(UBound(arrSplit)) 'Hent den sidste array, der må være filnavnet
if strNewFilename <> "" and instr(strFilename, ".") then
strExt = mid(strFilename, instr(strFilename, "."), len(strFilename) - instr(strFilename, ".") + 1)
strNewFilename = strNewFileName & strExt
strFilename = strNewFilename
end if
strFileSpec = Server.MapPath (LCase(strPath & strFilename))
strTemp = bin2str(MidB(bstrPostData, intStartPos, InStrB(intStartPos, bstrPostData, vbCrLfB) - intStartPos))
intStartPos = intStartPos + Len(strTemp) + 2
arrSplit = Split(strTemp, " ")
strContentType = arrSplit(UBound(arrSplit))
bolContentTypeOK = False
If arrAcceptType(LBound(arrAcceptType)) <> "" Then
For Each strTemp In arrAcceptType
If strContentType = strTemp Then
bolContentTypeOK = True
End If
Next
If Not bolContentTypeOK Then
'ContentType ikke fundet - Returner værdi og stop
FileUpload = 4
Exit Function
End If
End If
bolExtOK = False
If arrAcceptExt(LBound(arrAcceptExt)) <> "" Then
For Each strTemp In arrAcceptExt
If LCase(Right(strFilename, Len(strTemp))) = strTemp Then
bolExtOK = True
End If
Next
If Not bolExtOK Then
FileUpload = 5
Exit Function
End If
End If
intStartPos = intStartPos + 2
intEndPos = intEndPos - 2
intFileTotalBytes = intEndPos - intStartPos
If intMaxSize <1 Then
If intFileTotalBytes > intMaxSize Then
FileUpload = 6
Exit Function
End If
End If
Set fs = CreateObject("Scripting.FileSystemObject") 'Filsystem objekt
Set ts = fs.CreateTextFile(strFileSpec, True)
' For i = intStartPos To intEndPos - 1
' ts.Write(Chr(AscB(MidB(bstrPostData, i, 1)))) 'Skriv data eet tegn af gangen
' Next
dim intpakkeStr, intStep, ialt,encUd
intPakkeStr = 1024
if intFileTotalBytes > intPakkeStr then
intStep = intPakkeStr
else
intStep = intFileTotalBytes
end if
intPakkeStr = intStep
ialt = 0
i = intStartPos
Do Until i > intEndPos - 1
if i + intStep > intEndPos - 1 then
intPakkeStr = intEndPos - i
end if
ialt = ialt + intPakkeStr
ts.Write encodePakke(MidB(bstrPostData, i, intPakkeStr))
i = i + intStep
Loop
ts.Close 'Luk outputfil
Set f = fs.GetFile(strFileSpec)
If f.Size <> intFileTotalBytes Then
response.write f.size
FileUpload = 7
Exit Function
End If
FileUpload = 0
End If
End Function
Function bin2str(bstrBinary)
Dim i
For i = 1 To LenB(bstrBinary)
bin2str = bin2str & Chr(AscB(MidB(bstrBinary, i, 1)))
Next
End Function
%>