Jump to content

Upload Image WinHttp


Go to solution Solved by MadaraUchiha,

Recommended Posts

Hey,

I liek to upload a image file to my webserver without using FTP.

I tried this already ( '?do=embed' frameborder='0' data-embedContent>> ), it started without errors, also has shown me the msgbox with the headers, but 

the file doesn't appear on the server. Why it does not upload?

I am looking for a method to uplaod a file to my webserver, if possible very clean, without defining the headers etc, just a short pure Winhttp function to upload a file :/

Edited by MadaraUchiha

Link to comment
Share on other sites

I've seen multiple samples here on the forum but most of didn't work for me, and nearly all are using a HTML form to fill out.

Isn't it possible to send binary data to a PHP file?

I am looking for a win http (_WinHttpSendRequest or _WinHttpWriteData??) solution.

TCP and FTP is not an option for me.

Link to comment
Share on other sites

My PHP Script looks like this:

<?php
 move_uploaded_file($_FILES[$_GET["datei"]]['tmp_name'], $_GET["datei"]); 
?>

 

And the AutoIt script looks like this:

#include <Inet.au3>
$Pfad = "C:\Users\Domi\Desktop\Test.png"
_InetGetSource("http://[censored]/upload.php?datei="& $Pfad & "")

But nothing happens. When I run the script, I get no errors but the file doesn't appear on my webserver?

Edited by MadaraUchiha

Link to comment
Share on other sites

Hm, ok got a working PHP script...

<?
$upload = $_GET["upload"];
if ($upload == "now") {
  $path1 = "uploads/";
  $path="$path1".$_FILES['ufile']['name'];
  if($ufile !=none) {
    if(copy($_FILES['ufile']['tmp_name'], $path)) {
    echo "Success!<BR/>";
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"2; URL=uploads/\">";
    } else {
    echo "<font color=red><b>Error!</b></font>";
    }
  }
} else {
?>

<table width="250" border="0" align="left" cellpadding="0" cellspacing="1">
<tr>
<form action="?upload=now" method="post" enctype="multipart/form-data">
<td>
<table  border="0" cellpadding="3" cellspacing="1">
<tr>
<td>Select file
<input name="ufile" type="file" id="ufile" /></td>
</tr>
<tr>
<td align="left"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<a href="uploads">View Uploads</a>
<?   }   ?>

But i like to upload: 

1) With WinHTTP.au3

2) Using POST instead of GET

3) Without a form.

How? ;o

Link to comment
Share on other sites

Remove form part from your page and do something like this:

#include "WinHttp.au3"

Global Const $sFileToUpload = @ScriptFullPath ;<- Set correct file location here (current choice uploads this script)
Global Const $sAddress = "http://whatever.org/zz.php" ;<- Set correct address here

; Form to fill (page doesn't have transparent form)
Local $sForm = _
        '<form action="' & $sAddress & '?upload=now"" method="post" enctype="multipart/form-data">' & _
        '    <input type="file" name="ufile"/>' & _ ;
        '</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:ufile", $sFileToUpload)

If @error Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    MsgBox(64, "Success", "Result: " & @CRLF & $sHTML)
EndIf

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

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Remove form part from your page and do something like this:

#include "WinHttp.au3"

Global Const $sFileToUpload = @ScriptFullPath ;<- Set correct file location here (current choice uploads this script)
Global Const $sAddress = "http://whatever.org/zz.php" ;<- Set correct address here

; Form to fill (page doesn't have transparent form)
Local $sForm = _
        '<form action="' & $sAddress & '?upload=now"" method="post" enctype="multipart/form-data">' & _
        '    <input type="file" name="ufile"/>' & _ ;
        '</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:ufile", $sFileToUpload)

If @error Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    MsgBox(64, "Success", "Result: " & @CRLF & $sHTML)
EndIf

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

Tried it now.

Doesn't work:

My PHP Script:

<?
$upload = $_GET["upload"];
if ($upload == "now") {
  $path1 = "uploads/";
  $path="$path1".$_FILES['ufile']['name'];
  if($ufile !=none) {
    if(copy($_FILES['ufile']['tmp_name'], $path)) {
    echo "Success!<BR/>";
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"2; URL=uploads/\">";
    } else {
    echo "<font color=red><b>Error!</b></font>";
    }
  }
}
?>

AutoIt code:

#include "WinHttp.au3"

Global Const $sFileToUpload = @ScriptFullPath ;<- Set correct file location here (current choice uploads this script)
Global Const $sAddress = "http://whatever.org/zz.php" ;<- Set correct address here

; Form to fill (page doesn't have transparent form)
Local $sForm = _
        '<form action="' & $sAddress & '?upload=now"" method="post" enctype="multipart/form-data">' & _
        '    <input type="file" name="ufile"/>' & _ ;
        '</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:ufile", $sFileToUpload)

If @error Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    MsgBox(64, "Success", "Result: " & @CRLF & $sHTML)
EndIf

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

Error message:

sjqxot8f.png

Whats wrong? O_o

Edited by MadaraUchiha

Link to comment
Share on other sites

Ok, look.

I made a sample to upload a String using Post method to my php script and echo it in a txt file:

#include "WinHttp.au3"

    Opt("MustDeclareVars", 1)

    ; Initialize and get session handle
    Global $hOpen = _WinHttpOpen()
    ; Get connection handle
    Global $hConnect = _WinHttpConnect($hOpen,InputBox('Enter Host','Enter your Host without http:// , use www instead.' & @LF & "Sample www.myserver.com also don't put /Test.Php at the end!"))

    ; Simple-request it...
    MsgBox(64, "Result", _WinHttpSimpleRequest($hConnect, "POST", "/Test.php", Default, "Data=" & InputBox('String','String u like to send to the server')))

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

PHP:

<?php
  if( $_POST["Data"])
  {
   file_put_contents('Result.txt',$_POST['Data']);
     echo "Uploaded.";
     exit();
  }
?>

That works fine.

Now I tried to uplaod a file using 

FileRead(FileOpen(@ScriptDir & '\ufile.png',16))

instead of the string i enter in the inputbox.

It now uploads the bytes to the txt file... but I need to upload, so it will appear as image on the sever?

I am close to done, just this little thingy I need to fix, but how?

Link to comment
Share on other sites

okay... Got it now so far that the file appears, but it seems corrupted.

The bytes of the file which arrive on the server is twice times large than the original file...

#include "WinHttp.au3"

    Opt("MustDeclareVars", 1)

    ; Initialize and get session handle
    Global $hOpen = _WinHttpOpen()
    ; Get connection handle
    Global $hConnect = _WinHttpConnect($hOpen,InputBox('Enter Host','Enter your Host without http:// , use www instead.' & @LF & "Sample www.myserver.com also don't put /Test.Php at the end!"))

    ; Simple-request it...
    MsgBox(0,'Response',_WinHttpSimpleRequest($hConnect, "POST", "/Test.php", Default, "data=" & (FileRead(FileOpen(@ScriptDir & '\ufile.png',16)))))

    ; Close handles
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
<?php

 $Bytes = $_POST["data"];
 $fp = fopen('data.png', 'w+');
 fwrite($fp, $Bytes);
 fclose($fp);
 echo "Bien :)";
 exit();
 
?>

Link to comment
Share on other sites

With those php code problems i would use an other server instead of mine.
I can provide you an example of an image upload on a working server if that is what you need so you can go on with your life

P.S and try declaring fileread and fileopen functions outside the _WinHttpSimpleRequest

Edited by AutID
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...