Sign in to follow this  
Followers 0
MadaraUchiha

Upload Image WinHttp

29 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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.


Share this post


Link to post
Share on other sites

Bump.


Share this post


Link to post
Share on other sites

And your php page looks like?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Either learn php or find some example of php code which processes input correctly.

I believe php's documentation gives an example of how to do it correctly. Your code if fucking ridiculous.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

So it is the PHP Part which is not correct?

Or is it the Autoit part?


Share this post


Link to post
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


Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Remove the "else {" from the last line.

Did that, got another error, updated my post ...


Share this post


Link to post
Share on other sites

Whats wrong? O_o

Your server settings?

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Hm, how can I just upload to the main directory?


Share this post


Link to post
Share on other sites

You ask wrong questions. You ask dumb questions.

...Google the error you got.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

I did... I tried changing the path, same error... 

I really wonder why there is no simple fileupload function available for WinHttp.au3  :ermm:

Edited by MadaraUchiha

Share this post


Link to post
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?


Share this post


Link to post
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();
 
?>

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Yes, I would really like to see your example. Can u pm me?:)


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