Jump to content

Zip upload with AutoIt and Php


Recommended Posts

hello folks,

I want to upload a zip file from my computer to my server. The zip file is automatically extracted when it hit the server. The code is this

<?php

function rmdir_recursive($dir) {
    foreach(scandir($dir) as $file) {
       if ('.' === $file || '..' === $file) continue;
       if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
       else unlink("$dir/$file");
   }
   
   rmdir($dir);
}

if($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];
    
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        } 
    }
    
    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }

  /* PHP current path */
  $path = dirname(__FILE__).'/';  // absolute path to the directory where zipper.php is in
  $filenoext = basename ($filename, '.zip');  // absolute path to the directory where zipper.php is in (lowercase)
  $filenoext = basename ($filenoext, '.ZIP');  // absolute path to the directory where zipper.php is in (when uppercase)
    
  $targetdir = $path . $filenoext; // target directory
  $targetzip = $path . $filename; // target zip file
  
  /* create directory if not exists', otherwise overwrite */
  /* target directory is same as filename without extension */
  
  if (is_dir($targetdir))  rmdir_recursive ( $targetdir);
 
     
  mkdir($targetdir, 0777);
  
  
  /* here it is really happening */
    
    if(move_uploaded_file($source, $targetzip)) {
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);  // open the zip file to extract
        if ($x === true) {
            $zip->extractTo($targetdir); // place in the directory with same name  
            $zip->close();
    
            unlink($targetzip);
        }
        $message = "Your .zip file was uploaded and unpacked.";
    } else {    
        $message = "There was a problem with the upload. Please try again.";
    }
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unzip a zip file to the webserver</title>
</head>

<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

 

now i have a simple autoit code that opens a file in binary mode and read file. then upload zip to the server

#include <MsgBoxConstants.au3>


$sFilePath = @ScriptDir & '\test.zip'
Local $sFile = FileOpen($sFilePath, 16)
If $sFile = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf
$sFileRead = BinaryToString(FileRead($sFile))
FileClose($sFile)

$sBoundary = "---------------------------7da24f2e50046"

$sPD = '--' & $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="zip_file"; filename="test.zip"' & @CRLF & _
        'Content-Type: application/x-zip-compressed' & @CRLF & @CRLF & _
        $sFileRead & @CRLF & $sBoundary & '--'

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", "http://example.com/zipper.php", False)
$oHTTP.SetRequestHeader("Content-Type", 'multipart/related; boundary="' & $sBoundary & '"')
$oHTTP.Send(StringToBinary($sPD))
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

ConsoleWrite($oStatusCode & @CRLF)
ConsoleWrite($oReceived)

 

Now when the following autoit script is run, nothing happens(file is not uploaded). Where did i go wrong?

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