Sign in to follow this  
Followers 0
FireFox

Upload image with post request

1 post in this topic

#1 ·  Posted (edited)

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

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0