PHP Datei Upload funktioniert nicht...

  • Hallo Community,

    Wie gesagt mein PHP Datei Upload funktioniert nicht...
    Hab noch keine Ahnung wie das gehen soll, dass man da eine Datei anhängen kann... :)

    --FTP UDF möchte ich für diesen Zweck nicht nutzen..

    AutoIT Code

    Spoiler anzeigen
    [autoit]

    #include <ScreenCapture.au3>
    #include <File.au3>
    #include <IE.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $URL_ImgUpload = 'http://localhost/test/screen_upload.php'

    [/autoit] [autoit][/autoit] [autoit]

    $ScreenCapture_File_Temp = _TempFile(@TempDir,"~",".bmp",10)
    $ScreenCapture_Bmp = _ScreenCapture_Capture ("")
    _ScreenCapture_SaveImage ($ScreenCapture_File_Temp, $ScreenCapture_Bmp)

    [/autoit] [autoit][/autoit] [autoit]

    $ScreenCapture_IE = _IECreate($URL_ImgUpload)
    $ScreenCapture_Form = _IEFormGetObjByName($ScreenCapture_IE, "form")
    $ScreenCapture_Form_File = _IEFormElementGetObjByName($ScreenCapture_Form, "img_file")
    _IEFormElementSetValue ($ScreenCapture_Form_File, $ScreenCapture_File_Temp)
    _IEFormSubmit ($ScreenCapture_Form)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit]

    PHP Code

    Spoiler anzeigen

    Danke im Voraus!

    mfg. Andi

  • Fileupload-Felder können im IE nicht automatisch befüllt werden. Das müsstest du über Send / Controlsend machen, in der Hilfe müsste auch ein Beispiel sein.
    Du kannst aber auch ohne den IE, mit Hilfe der WinHTTP.au3 die Dateien per POST hochladen, aber das ist schwerer, dazu muss man sich mit dem HTTP-Protokoll einigermaßen auskennen.

  • Fileupload-Felder können im IE nicht automatisch befüllt werden. Das müsstest du über Send / Controlsend machen, in der Hilfe müsste auch ein Beispiel sein.
    Du kannst aber auch ohne den IE, mit Hilfe der WinHTTP.au3 die Dateien per POST hochladen, aber das ist schwerer, dazu muss man sich mit dem HTTP-Protokoll einigermaßen auskennen.


    Hmm... Schade.
    Könntest du mir vlt. mit der WinHTTP helfen?

  • Teste mal das hier:
    http://www.autoitscript.com/forum/index.php?showtopic=84133&st=20&p=610967&#entry610967
    Und hier siehst du das Format für POST etwas besser:
    http://www.autoitscript.com/forum/index.php?showtopic=84133&st=80&p=706293&#entry706293
    (es gibt auch ein einfacheres Format, aber dann kann man keine Dateien senden)

    :S seh mir die WinHTTP Skripte jetzt schon eine 1/2 Stunde durch, aber irgndwie werd ich nicht draus schlau, wie man damit so einen POST schreiben könnte...

  • :S seh mir die WinHTTP Skripte jetzt schon eine 1/2 Stunde durch, aber irgndwie werd ich nicht draus schlau, wie man damit so einen POST schreiben könnte...


    Es sollte funktionieren, wenn du die WinHTTP.au3 nimmst und das Beisüiel aus dem ersten Link ab der Zeile

    [autoit]

    If Not IsDeclared("MIMETypes") Then Global $MIMETypes

    [/autoit]

    Dann geht der Post mit

    [autoit]

    Local $form_fields[2][2] = [["password", "PW123"],["filename", "test.bmp"]] ; normale Form-Felder, also alles, was keine Datei ist ( Name -> Wert)
    Local $form_files[1][2] = [["fileupload", "D:\test.bmp"]] ; Dateifelder, mit Name und Pfad zur Datei

    [/autoit][autoit][/autoit][autoit]

    $x = post_multipart("http://www.bla.de/hochladen.php", "", $form_fields, $form_files) ; senden
    ConsoleWrite(">Body: " & @CRLF & $x[1] & @CRLF) ; Ergebnis ausgeben

    [/autoit]

  • Es sollte funktionieren, wenn du die WinHTTP.au3 nimmst und das Beisüiel aus dem ersten Link ab der Zeile

    [autoit]

    If Not IsDeclared("MIMETypes") Then Global $MIMETypes

    [/autoit]

    Dann geht der Post mit

    [autoit]

    Local $form_fields[2][2] = [["password", "PW123"],["filename", "test.bmp"]] ; normale Form-Felder, also alles, was keine Datei ist ( Name -> Wert)
    Local $form_files[1][2] = [["fileupload", "D:\test.bmp"]] ; Dateifelder, mit Name und Pfad zur Datei

    [/autoit] [autoit][/autoit] [autoit]

    $x = post_multipart("http://www.bla.de/hochladen.php", "", $form_fields, $form_files) ; senden
    ConsoleWrite(">Body: " & @CRLF & $x[1] & @CRLF) ; Ergebnis ausgeben

    [/autoit]


    ... habs jetzt mal versucht ;)
    Geht aber irgendwie nicht...

    AutoIt Code

    Spoiler anzeigen
    [autoit]

    #include-once
    #include <WinHTTP.au3>
    #include <Array.au3>
    #include <ScreenCapture.au3>
    #include <File.au3>
    #include <IE.au3>
    If Not IsDeclared("MIMETypes") Then Global $MIMETypes

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $USE_MIMETYPEFILE = 0

    [/autoit] [autoit][/autoit] [autoit]

    Global $MIMETypes[8][2] = [ _
    ["bmp", "image/bmp"], _
    ["gif", "image/gif"], _
    ["jpe", "image/jpeg"], _
    ["jpeg", "image/jpeg"], _
    ["jpg", "image/jpeg"], _
    ["png", "image/png"], _
    ["tif", "image/tiff"], _
    ["tiff", "image/tiff"]]

    [/autoit] [autoit][/autoit] [autoit]

    $ScreenCapture_File_Temp = _TempFile(@TempDir,"~",".bmp",10)
    $ScreenCapture_Bmp = _ScreenCapture_Capture ("")
    _ScreenCapture_SaveImage ($ScreenCapture_File_Temp, $ScreenCapture_Bmp)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;~ Local $form_fields[2][2] = [["password", "PW123"],["filename", "test.bmp"]]
    Local $form_fields[2][2] = [["password", "PW123"],["filename", "test.bmp"]] ; normale Form-Felder, also alles, was keine Datei ist ( Name -> Wert)
    Local $form_files[1][2] = [["img_file", $ScreenCapture_File_Temp]] ; Dateifelder, mit Name und Pfad zur Date

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $x = post_multipart('http://localhost/WEBBER/screen_upload.php', "", $form_fields, $form_files) ; senden
    ConsoleWrite(">Body: " & @CRLF & $x[1] & @CRLF) ; Ergebnis ausgeben

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;~ ConsoleWrite(">Header: " & @CRLF & $x[0] & @CRLF & @CRLF)
    ;~ ConsoleWrite(">Body: " & @CRLF & $x[1] & @CRLF)
    If Not IsDeclared("MIMETypes") Then Global $MIMETypes
    If Not IsDeclared("USE_MIMETYPEFILE") Then $USE_MIMETYPEFILE = 1

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ;===============================================================================
    ;
    ; Name...........: _WinHttpWriteDataBin
    ; Description ...: Writes request data to an HTTP server.
    ; Syntax.........: _WinHttpWriteData($hRequest, $string)
    ; Parameters ....: $hRequest - Valid handle returned by _WinHttpSendRequest().
    ; $binary - Binary data to write.
    ; Return values .: Success - Returns 1
    ; - Sets @error to 0
    ; - sets @extended to written bytes
    ; Failure - Returns 0 and sets @error:
    ; |1 - DllCall failed.
    ; Author ........: trancexx
    ; Modified.......: ProgAndy
    ; Remarks .......:
    ; Related .......:
    ; Link ..........; http://msdn.microsoft.com/en-us/library/aa384120(VS.85).aspx
    ; Example .......; Yes
    ;
    ;==========================================================================================
    Func _WinHttpWriteDataBin($hRequest, $binary)
    Local $lpBinary
    Local $iNumberOfBytesToWrite
    If IsDllStruct($binary) Then
    $lpBinary = DllStructGetPtr($binary)
    $iNumberOfBytesToWrite = DllStructGetSize($binary)
    Else
    $iNumberOfBytesToWrite = BinaryLen($binary)
    Local $sBinary = DllStructCreate("byte[" & $iNumberOfBytesToWrite & "]")
    DllStructSetData($sBinary, 1, $binary)
    $lpBinary = DllStructGetPtr($sBinary)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Local $a_iCall = DllCall("Winhttp.dll", "int", "WinHttpWriteData", _
    "hwnd", $hRequest, _
    "ptr", $lpBinary, _
    "dword", $iNumberOfBytesToWrite, _
    "dword*", 0)

    [/autoit] [autoit][/autoit] [autoit]

    If @error Or Not $a_iCall[0] Then
    Return SetError(1, 0, 0)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Return SetError(0, $a_iCall[4], 1)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_WinHttpWriteDataBin

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    post_multipart and encode_multipart_formdata translated from the cookbook
    see ActiveState's ASPN
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func post_multipart($host, $selector, ByRef $fields, ByRef $files)
    ;~ """
    ;~ Post fields and files to an http host as multipart/form-data.
    ;~ fields is a sequence of (name, value) elements for regular form fields.
    ;~ files is a sequence of (name, filename, value) elements for data to be uploaded as files
    ;~ Return the server's response page.
    ;~ """
    Local $Return = encode_multipart_formdata($fields, $files)
    Local $content_type = 'Content-Type: ' & $Return[0] & @CRLF
    ;~ $body = $Return[1]
    Local $URL = _WinHttpCrackUrl($host)
    Local $hSession = _WinHttpOpen()
    Local $hConnection = _WinHTTPConnect($hSession,$URL[2],$URL[3])
    ConsoleWrite( $content_type&@CRLF)
    ;~ ConsoleWrite( StringReplace($Return[1],Chr(0),".")&@CRLF)
    ;~ $s = StringToBinary($Return[1])
    ;~ ConsoleWrite( BinaryLen($s) & "--" & StringLen($Return[1]))
    ;~ Return
    Local $hRequest = _WinHttpOpenRequest($hConnection,"POST",$URL[6]&$URL[7],"HTTP/1.1","http://"&$URL[2])
    _WinHttpSendRequest($hRequest,$content_type,$WINHTTP_NO_REQUEST_DATA,StringLen($Return[1]));,StringLen($Return[1]))
    _WinHTTPWriteDataBin($hRequest,StringToBinary($Return[1]))
    MsgBox(0, '', @error)
    _WinHttpReceiveResponse($hRequest)
    Local $Return[2]
    If _WinHttpQueryDataAvailable($hRequest) Then
    Local $temp
    While 1
    $temp = _WinHttpReadData($hRequest)
    If $temp = "" Then ExitLoop
    $Return[1] &=$temp
    WEnd
    $temp =""
    ; Does not work since @error is 0, when no more data is available
    ;~ Do
    ;~ $Return[1] &= _WinHttpReadData($hRequest)
    ;~ Until @error <> 0
    EndIf
    $Return[0] = _WinHttpQueryHeaders($hRequest)
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnection)
    _WinHttpCloseHandle($hSession)
    Return $Return
    EndFunc ;==>post_multipart

    [/autoit] [autoit][/autoit] [autoit]

    ; Prog@ndy
    Func encode_multipart_formdata($fields, $files)
    ;~ """
    ;~ fields is a sequence of (name, value) elements for regular form fields.
    ;~ files is a sequence of (name, filename, value) elements for data to be uploaded as files
    ;~ Return (content_type, body) ready for httplib.HTTP instance
    ;~ """
    Local Const $BOUNDARY = 'ThIs_Is_tHe_bouNdaRY_$'
    ;~ CRLF = '\r\n'
    $L = ""
    For $i = 0 To UBound($fields) - 1
    $L &= ('--' & $BOUNDARY) & @CRLF
    $L &= ('Content-Disposition: form-data; name="' & $fields[$i][0] & '"') & @CRLF
    $L &= @CRLF
    $L &= $fields[$i][1] & @CRLF
    Next
    For $i = 0 To UBound($files) - 1
    $L &= ('--' & $BOUNDARY) & @CRLF
    $L &= ('Content-Disposition: form-data; name="' & $files[$i][0] & '"; filename="' & $files[$i][1] & '"') & @CRLF
    $content_type = get_content_type($files[$i][1])
    $L &= ('Content-Type: ' & $content_type) & @CRLF
    $L &= @CRLF
    ;~ If StringLeft($content_type, 5) <> "text/" Then
    ;~ $f = FileOpen($files[$i][1], 16)
    ;~ $L &= BinaryToString(FileRead($f)) & @CRLF
    ;~ FileClose($f)
    ;~ Else
    $L &= FileRead($files[$i][1]) & @CRLF
    ;~ EndIf
    Next
    $L &= ('--' & $BOUNDARY & '--') & @CRLF
    $L &= @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    $content_type = 'multipart/form-data; boundary="' & $BOUNDARY & '"'
    Local $Return[2] = [$content_type, $L]
    ;~ return content_type, body
    Return $Return
    EndFunc ;==>encode_multipart_formdata

    [/autoit] [autoit][/autoit] [autoit]

    ; Prog@ndy
    Func get_content_type($path)
    ;~ Return "application/octet-stream"
    Local $szExt = StringLower(StringRegExpReplace($path,".*(?:\.([^.\\/]*))?\Z","$1"))
    ;~ ConsoleWrite(StringTrimLeft($szExt, 1) & @CRLF)
    If $szExt = "" Then Return 'application/octet-stream'
    Local $mimeid = _ArrayBinarySearch2D($MIMETypes, $szExt)
    ;~ ConsoleWrite($mimeid & @error & @CRLF)
    If $mimeid = -1 Then Return SetError(1, 0, 'application/octet-stream')
    Return $MIMETypes[$mimeid][1]
    EndFunc ;==>get_content_type
    ;===============================================================================
    ;
    ; Function Name: _ArrayBinarySearch()
    ; Description: Uses the binary search algorithm to search through a
    ; 1-dimensional array.
    ; Author(s): Jos van der Zande <jdeb at autoitscript dot com>
    ; Modified: Prog@ndy
    ;
    ;===============================================================================
    Func _ArrayBinarySearch2D(Const ByRef $avArray, $vValue, $iStart = 0, $Column = 0, $iEnd = 0)
    If Not IsArray($avArray) Then Return SetError(1, 0, -1)

    [/autoit] [autoit][/autoit] [autoit]

    Local $iUBound = UBound($avArray) - 1

    [/autoit] [autoit][/autoit] [autoit]

    ; Bounds checking
    If $iEnd < 1 Or $iEnd > $iUBound Then $iEnd = $iUBound
    If $iStart < 0 Then $iStart = 0
    If $iStart > $iEnd Then Return SetError(4, 0, -1)

    [/autoit] [autoit][/autoit] [autoit]

    Local $iMid = Int(($iEnd + $iStart) / 2)

    [/autoit] [autoit][/autoit] [autoit]

    If $avArray[$iStart][$Column] > $vValue Or $avArray[$iEnd][$Column] < $vValue Then Return SetError(2, 0, -1)

    [/autoit] [autoit][/autoit] [autoit]

    ; Search
    While $iStart <= $iMid And $vValue <> $avArray[$iMid][$Column]
    If $vValue < $avArray[$iMid][$Column] Then
    $iEnd = $iMid - 1
    Else
    $iStart = $iMid + 1
    EndIf
    $iMid = Int(($iEnd + $iStart) / 2)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    If $iStart > $iEnd Then Return SetError(3, 0, -1) ; Entry not found

    [/autoit] [autoit][/autoit] [autoit]

    Return $iMid
    EndFunc ;==>_ArrayBinarySearch2D

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit]

    SciTE Console

    Spoiler anzeigen
  • Du musst ja auch den Feldnamen anpassen ;)
    "img_file" --> das heiß bei dir 'bild', wenn ich das richtig sehe.
    //Edit: ja. Im PHP heißt es 'bild', in der Form 'img_file'. Das muss einheitlich sein.

    PS: $form_fields kannst du auf 0 setzen, wenn du keine Felder hast. ($form_fields = 0)

  • Du musst ja auch den Feldnamen anpassen ;)
    "img_file" --> das heiß bei dir 'bild', wenn ich das richtig sehe.
    //Edit: ja. Im PHP heißt es 'bild', in der Form 'img_file'. Das muss einheitlich sein.

    PS: $form_fields kannst du auf 0 setzen, wenn du keine Felder hast. ($form_fields = 0)


    yeah!
    geil jetzt funzts

    JUHU!


    :rock:;):P:D:D:rock:;):P:D:D