Jump to content
nylle

cant get http upload for pictures working

Recommended Posts

nylle

hello

ive been trying to get the below snippet to work with jpg's created by autoits screencapture function.

its working flawless with text files but when i download the jpg again i cant view it and the text-view is like 95% "???????????.." and so on.

is there any other solution besides this snippet or could anyone please assist me with this issue?

the php part and everything else is fine. at least i assume so - using sort of the phpcode from the thread i linked below.

edit: i just realized that it might be better if i just put the code here instead of linking the thread:

autoit:

#include <HTTP.au3>
$host = "www.testbed.loc"
$port = "443"
$page = "test_script.php"
$vars = "action=upload"

$url = $page&"?"&_HTTPEncodeString($vars)
$socket = _HTTPConnect($host, $port)
$get = _HTTPPost_File($host,$url,$socket,"testuploadfile3.doc", "uploadedfile")
$recv = _HTTPRead($socket,1)
MsgBox(0, "Info", "Data received:" & @CRLF & $recv[4] & @CRLF)


Func _HTTPPost_File($host, $page, $socket = -1, $file = "", $fieldname = "")
    Dim $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    $contenttype = _HTTPPost_contenttype($file)
    
; Maybe this can be done easier/better?
    $boundary = "------"&Chr(Random(Asc("A"), Asc("Z"), 3))&Chr(Random(Asc("a"), Asc("z"), 3))&Chr(Random(Asc("A"), Asc("Z"), 3))&Chr(Random(Asc("a"), Asc("z"), 3))&Random(1, 9, 1)&Random(1, 9, 1)&Random(1, 9, 1)
    
    $fileopen = FileOpen($file, 0); Open in read only mode
    $fileread = FileRead($fileopen)
    FileClose($fileopen)
        
    $extra_commands = "--"&$boundary&@CRLF
    $extra_commands &= "Content-Disposition: form-data; name="""&$fieldname&"""; filename=""" & $file & """" &@CRLF
    $extra_commands &= "Content-Type: "&$contenttype&@CRLF&@CRLF
    $extra_commands &= $fileread&@CRLF
    $extra_commands &= "--"&$boundary&"--"
    
    Dim $datasize = StringLen($extra_commands)
    
    $command = "POST "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= "Content-Type: multipart/form-data; boundary="&$boundary&@CRLF
    $command &= "Content-Length: "&$datasize&@CRLF&@CRLF
    $command &= $extra_commands
    
;debug info
    If FileExists("output.txt") Then FileDelete("output.txt")
    $file = FileOpen("output.txt", 1)
    FileWrite($file, $command)
    FileClose($file)
;end debug info
    
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
EndFunc

Func _HTTPPost_contenttype($file = "")
    $fileextension = StringRight($file,4)
; I blieve these are the only 2 types that matter when uploading
    Switch $fileextension
        Case ".txt"
            $contenttype = "text/plain"
        Case Else
            $contenttype = "application/octet-stream"
        EndSwitch
    Return $contenttype
EndFunc

PHP:

if($_GET['action'] == 'upload'){
$target_path = "data/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
}

 

using this https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=13776 HTTP.au3

regards and thanks in advance

Edited by nylle

Share this post


Link to post
Share on other sites
trancexx

You want to upload to https? That UDF can't handle that.

But, you can do it this way:

#include "WinHttp.au3"

$sAddress = "http://www.testbed.loc:443/test_script.php?action=upload" ; target

$sFileToUpload = "testuploadfile3.doc"


$sForm = _
        '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
        '    <input type="file" name="uploadedfile"/>' & _ ;
        '</form>'

; Initialize and get session handle
$hOpen = _WinHttpOpen()

$hConnect = $sForm ; will pass form as string so this is for coding correctness because $hConnect goes in byref

; Fill form
$sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, Default, "name:uploadedfile", $sFileToUpload)

If @error Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    ConsoleWrite($sHTML & @CRLF)
    MsgBox(0, "Info", "Data received:" & @CRLF & $sHTML & @CRLF)
EndIf

; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

Just set correct address. I left what that guy in the other thread used, which doesn't make sense because port 433 is specified for http.
It should be either

$sAddress = "https://your.domain.com/test_script.php?action=upload" ; target

Or

$sAddress = "http://your.domain.com/test_script.php?action=upload" ; target

...for normal configurations.

  • Like 1

♡♡♡

.

eMyvnE

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

×