Jump to content

Upload image with post request


Recommended Posts

Hi,

I want to upload image to my website with a post request

Here is the source of the index.php where I want to submit request :

<?php
if(!empty($_FILES)) {
move_uploaded_file($_FILES['file']['tmp_name'], './'.$_FILES['file']['name']);
}
?>

<form method="post" action="" enctype="multipart/form-data"> 
    <input type="file" name="file" />
    <input type="submit" />
</form>

With an addon of firefox (temperdata) I can get the post request which is :

-----------------------------4827543632391
\r\n
Content-Disposition: form-data; name="file"; filename="mypic.jpg"
\r\n
Content-Type: image/png
\r\n\r\n
IMAGE DATA HERE IN TEXT
\r\n-----------------------------4827543632391--\r\n

And this is my script for the moment :

#include "Date.au3"

$hOpen = FileOpen("mypic.jpg", 128)
$sContent = FileRead($hOpen)
FileClose($hOpen)

_INetGetSourcePostImage("http://example.com/index.php", $sContent)

Func _INetGetSourcePostImage($sURL, $sImgContent)
    Local $iSocket, $sHeader, $sRecv, $iIP, $sHost, $aRegExp, $sHttp1, $iErr, $iSend, $aReturn[2]

    If $sURL = '' Or $sImgContent = '' Then Return SetError(1, 0, 0)

    If StringLeft($sURL, 7) <> 'http://' And StringLeft($sURL, 8) <> 'https://' Then $sURL = "http://" & $sURL
    If StringRight($sURL, 1) <> "/" Then $sURL &= "/"

    $aRegExp = StringRegExp($sURL, "http?://(.*?)/", 3)
    If @error Then Return SetError(2, 0, 0)

    $sHost = $aRegExp[0]
    If $sHost = '' Then Return SetError(3, 0, 0)

    $sHttp1 = StringTrimLeft($sURL, StringInStr($sURL, "/", -1, 3) - 1)
    If $sHttp1 = '' Then Return SetError(3, 0, 0)

    $boundary = "-----------------------------" + _TimeToTicks(@HOUR, @MIN, @SEC)

    $sHeader = "POST " & $sHttp1 & " HTTP/1.1" & @CRLF & _
            "Host: " & $sHost & @CRLF & _
            "User-Agent: AutoIt v3" & @CRLF & _
            "--" & $boundary & @CRLF & "Content-Disposition: form-data; name=""{0}""; filename=""{1}""" & @CRLF & _
            "Content-Type: multipart/form-data; boundary=" + $boundary & @CRLF & @CRLF & _
            $sContent & @CRLF & $boundary & "--" & @CRLF
;~          "Content-Length: " & StringLen($sPost) & @CRLF & @CRLF & $sPost & @LF

    TCPStartup() ;If not already done
    $iIP = TCPNameToIP($sHost)
    If $iIP = '' Or StringInStr($iIP, ".") = 0 Then Return SetError(4, 0, 0)
    $iSocket = TCPConnect($iIP, 80)
    If @error Or $iSocket < 0 Then Return SetError(5, 0, 0)

    $iSend = TCPSend($iSocket, $sHeader)
    If @error Or $iSend < 1 Then Return SetError(6, 0, 0)


    While 1
        $sRecv = TCPRecv($iSocket, 1024)
        $iErr = @error
        If $sRecv <> '' Then
            While 1
                $sRecv &= TCPRecv($iSocket, 1024)
                If @error Then ExitLoop 2
            WEnd
        EndIf
        If $iErr Then Return SetError(7, 0, 0)
    WEnd

    $aReturn[0] = StringLeft($sRecv, StringInStr($sRecv, @CRLF & @CRLF) - 1)
    $aReturn[1] = StringTrimLeft($sRecv, StringLen($aReturn[0]) + 4)
    Return $aReturn
EndFunc   ;==>_INetGetSourcePostImage

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...