Sign in to follow this  
Followers 0
turbov21

Need help uploading a file

1 post in this topic

#1 ·  Posted (edited)

I have two scripts working together, my client script:

#NoTrayIcon

AutoItSetOption("MustDeclareVars", 1)

Dim $url = "http://domain.tld/u/index.php", $boundary = "---------BoUnDaRy!";
Dim $file = "C:\\andy\\dia.png", $body = "";
Dim $fileparts = StringSplit($file, "\")
Dim $filename = $fileparts[UBound($fileparts) - 1]

Dim $oHttp = _CreateMSXMLObj()
If $oHttp = 0 Then _ErrorMsg("Could not create MSXML object")

$body &= "--" & $boundary & @CRLF
$body &= "Content-Disposition: form-data; name=""file""" & @CRLF
$body &= @CRLF
$body &= $filename & @CRLF
$body &= "--" & $boundary & @CRLF
$body &= "Content-Disposition: form-data; name=""file""; filename=""" & $filename & """" & @CRLF
$body &= "Content-Type: application/octet-stream" & @CRLF
$body &= "Content-Transfer-Encoding: binary" & @CRLF
$body &= @CRLF
$body &= _LoadFile($file) & @CRLF
$body &= "--" & $boundary & "--" & @CRLF

MsgBox(0, "Body", $body)

$oHttp.Open("POST", $url, False)
$oHttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=" & $boundary)
$oHttp.setRequestHeader("Content-Length", StringLen($body))
$oHttp.Send($body)

MsgBox(0, "Response", $oHttp.responseText)

Func _LoadFile($file)
    Dim $fh = FileOpen($file, 0)
    Dim $fc
    While 1
        $fc &= FileRead($fh, 4096)
        If @error = -1 Then ExitLoop
    Wend
    FileClose($fh)
    Return $fc
EndFunc

; Ganked and modified from: http://www.autoitscript.com/forum/index.php?showtopic=77155
Func _CreateMSXMLObj()
    Dim $xmlObj = ObjCreate("Msxml2.XMLHTTP.6.0")
    If IsObj($xmlObj) Then Return $xmlObj
       
    $xmlObj = ObjCreate("Msxml2.XMLHTTP.3.0")
    If IsObj($xmlObj) Then Return $xmlObj
       
    $xmlObj = ObjCreate("Msxml2.XMLHTTP")
    If IsObj($xmlObj) Then Return $xmlObj
       
    $xmlObj = ObjCreate("Microsoft.XMLHTTP")
    If IsObj($xmlObj) Then Return $xmlObj
       
    Return 0
EndFunc

; This is here so if there's an error, the program exits
Func _ErrorMsg($msg)
    MsgBox(0, "Error", $msg)
    Exit
EndFunc

...and the PHP server script:

<?php

$method = strtolower($_SERVER['REQUEST_METHOD']);

if ($method == "post") {
    $file = isset($_POST['file']) ? basename($_POST['file']) : null;
    if ($file == null) { Error("No file"); }
    if (!isset($_FILES['file'])) { Error("No file uploaded."); }
    if (!move_uploaded_file($_FILES['file']['tmp_name'], "files/" . $file)) {
        print_r($_FILES['file']);
        Error("Could not move file.\n\n");
    }
    
    header("Location: index.php");
}
else {
    $files = glob("files/*");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
    <head>
        <title> Files </title>
    </head>
    <body>
        <h2>Files:</h2>
        <ul>
<?php if (count($files) > 0) { ?>
<?php foreach($files as $file) { ?>
            <li><a href="<?php echo($file); ?>"><?php echo(basename($file)); ?></a></li>
<?php } ?>
<?php } else { ?>
            <li>No files.</li>
<?php } ?>
        </ul>
    </body>
</html>
<?php }

function Error($msg) { ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
    <head>
        <title> Error </title>
    </head>
    <body>
        <h2>ERROR: <?php echo(isset($msg) ? $msg : "No error message."); ?></h2>
    </body>
</html>
<?php

    exit;
}
?>

My client and server script work up to a certain point. I can upload text files, but binary files (ala PNG's) are giving me an error on the server side. That error is, "The uploaded file was only partially uploaded." Can someone tell me what I'm doing wrong here? I've tried opening files with 4 (raw) and 16 (binary), and while those will upload, they're not in a binary format.

Any help would be hot. I'm trying to avoid having to use the TCP functions if at all possible. Thanks!

Edited by turbov21

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