mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Feb 2007
Posts: 91
S
spermis Offline OP
Babel fish
OP Offline
Babel fish
S
Joined: Feb 2007
Posts: 91
Hello.
I was wondering, could someone make a script that would upload multiple files to a directory?

For example i write /upload file1.jpg file2.exe file3.txt file4.txt file5.jpg , and it uploads all of them to a directory on a webserver, and then echo'es in mirc - Done uploading files!

All of the upload scripts I have seen can upload only one file. could someone help me out?



/*
How the script is currently configured it works like this (EXAMPLE):
-The user types '/upload test.exe' (test.exe is in the mirc dir)
-The script will submit the file to www.google.com/upload.php
-The php file will move the file 'test.exe' to the dir 'upload/'
-The file can be found at 'www.google.com/upload/test.exe'
-This is an example

-- CONTENT OF THE PHP FILE NAMED 'upload.php' (remember the mirc script must point to this file --
<?php
//Set uploaddir, remember that for that folder you must set read+write permission,
//look for the command CHMOD and set the permissions in the folder to '777'
$uploaddir = "upload/";

$file = $uploaddir . $_FILES['uploadfile']['name'];
move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)
?>
--------------------------------------------------------------------------------------------------
*/



alias upload {

;-- Set these variables --
%hostofphpfile = www.google.com
%locationofphpfile = /upload.php
;-------------------------

%dest = $$1-
if ((!$file(%dest)) || ($isdir(%dest))) return
.remove upload.tmp
echo -a Uploading file %dest $+ ....
sockopen upload %hostofphpfile 80
}
On *:sockopen:upload:{

var %b = $md5() | write -c upload.tmp $+(--,%b,$lf,$&
Content-Disposition: form-data; name="uploadfile";,$&
filename=",$nopath(%dest),",$lf,$lf)
.copy -a " $+ %dest $+ " upload.tmp
write upload.tmp -- $+ %b

var %write = sockwrite -tn upload
%write POST %locationofphpfile HTTP/1.1
%write Host: %hostofphpfile
%write Connection: close
%write Content-Type: multipart/form-data; boundary= $+ %b
%write Content-Length: $file(upload.tmp)
%write
.fopen upload upload.tmp
}

On *:sockwrite:upload:{
if $sock(upload).sq > 8192 || !$fopen(upload) { return }
if $fread(upload,8192,&a) {
sockwrite upload &a
}
else .fclose upload
}

On *:sockread:upload:{
var %temp
sockread %temp
if & 2* iswm %temp {
echo 3 -a SUCCESS!
}
elseif & 4* iswm & temp {
echo 4 -a FAILED!
}
}

On *:sockclose:upload:cleanupload
alias cleanupload {
.sockclose upload
if $fopen(upload) { .fclose upload }
unset %dest
.remove upload.tmp
}

Last edited by spermis; 20/04/12 02:53 PM.
Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
The only thing which needs to be changed is the generation of the upload.tmp file.

The content of this file would resemble the following:

Code:
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y

--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of file2.gif...
--BbC04y--
--AaB03x--


For multiple files, a multipart/mixed section is embedded within the multipart/form-data. This part has a separate boundary.

The section which creates this file in your existing script is at the beginning of the sockopen event. You will need to rewrite/replace it with something similar to the following; of course replace variables and otherwise change the function as is necessary to incorporate it into your script. The %boundary used in this function must be the same one used in the header.

Code:
alias prep.upload {
  var %upload = upload.tmp
  var %files = "file1.txt":file2.bin
  var %boundary = $md5(%files), %mixedboundary = $md5(%boundary)
  
  write -c %upload $+(--,%boundary,$crlf)
  write %upload Content-Disposition: form-data; name="files"
  write %upload Content-Type: multipart/mixed; $+(boundary=,%mixedboundary,$crlf,$crlf)

  var %i = 1
  while ($gettok(%files,%i,58)) {
    write %upload $+(--,%mixedboundary)
    write %upload Content-Disposition: file; $+(filename=,$qt($nopath($v1)))
    write %upload $+(Content-Type: application/octet-stream,$crlf,$crlf)
    .copy -a $qt($v1) %upload
    inc %i
  }
  
  write %upload $+(--,%mixedboundary,--)
  write %upload $+(--,%boundary,--)
}


Although based on my existing sockets, this is untested as I don't have a known server to work against which accepts multiple file uploads. If you can provide one, I will try to iron out any bugs.

Edit: After actually checking it out, Chrome is using multiple form-data entries and not using multipart/mixed at all. Please provide a known site where I may test this. Chrome is building the request as follows:

Code:
alias prep.upload {
  var %upload = upload.tmp
  var %files = "file1.txt":file2.bin
  var %boundary = $md5(%files)
  
  write -c %upload
  var %i = 1
  while ($gettok(%files,%i,58)) {
    write %upload $+(--,%boundary)
    write %upload Content-Disposition: form-data; name="files"; $+(filename=,$qt($nopath($v1)))
    write %upload $+(Content-Type: application/octet-stream,$crlf,$crlf)
    .copy -a $qt($v1) %upload
    inc %i
  }
  
  write %upload $+(--,%boundary,--)
}

Last edited by Loki12583; 20/04/12 07:32 PM.
Joined: Feb 2007
Posts: 91
S
spermis Offline OP
Babel fish
OP Offline
Babel fish
S
Joined: Feb 2007
Posts: 91
I PM'ed you info with site information.

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
Got it working, except it (apparently) does not work in mIRC 6.21. Does anyone have ideas about what could be causing that?

Code:
<?php
$uploads_dir = 'uploads';
foreach ($_FILES["files"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["files"]["tmp_name"][$key];
        $name = $_FILES["files"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>
<form action='upload.php' method='post' enctype='multipart/form-data'>
  <input name='files[]' type=file multiple>
  <input type='submit'>
</form>

Code:
alias upload {
  var %host = HOST
  var %page = upload.php
  var %port = 80

  var %sockname = upload $+ $ticks $+ $rand(1111,9999)
  var %i = 1, %n = $0, %files
  while (%i <= %n) {
    if ($file($ [ $+ [ %i ] ]) != $null) {
      var %files = $addtok(%files,$ [ $+ [ %i ] ],58)
    } 
    inc %i
  }
  if (!%files) return

  hmake %sockname
  hadd %sockname page %page
  hadd %sockname name files[]
  hadd %sockname files %files

  sockopen %sockname %host %port
}

alias prep.upload {
  var %upload = $1
  var %files = $2
  var %boundary = $md5(%files)

  write -c %upload
  var %i = 1
  while ($gettok(%files,%i,58)) {
    var %file = $v1
    bwrite %upload -1 -1 $+($iif(%i > 1,$crlf),--,%boundary)
    write %upload Content-Disposition: form-data; $+(name=",$hget($sockname,name),";) $+(filename=,$qt($nopath(%file)))
    write %upload $+(Content-Type: application/octet-stream,$crlf,$crlf)
    .copy -a $qt(%file) %upload
    inc %i
  }

  bwrite %upload -1 -1 $+($crlf,--,%boundary,--)
  return %boundary
}

on *:sockopen:upload*:{
  var %host = $sock($sockname).addr
  var %page = $hget($sockname,page)
  var %files = $hget($sockname,files)
  var %boundary = $prep.upload($sockname,%files)

  var %a = sockwrite -tn $sockname
  %a POST %page HTTP/1.1
  %a Host: %host
  %a Connection: close
  %a Content-Type: multipart/form-data; boundary= $+ %boundary
  %a Content-Length: $file($sockname)
  %a
  .fopen $sockname $sockname
}

on *:sockwrite:upload*:{
  var %size = $calc(16384 - $sock($sockname).sq)
  if (%size <= 0) || (!$fopen($sockname)) {
    return
  }

  if ($fread($sockname,%size,&upload)) {
    sockwrite $sockname &upload
  }
  elseif ($fopen($sockname).fname) {
    .fclose $sockname
    .remove $v1
  }
}

on *:sockread:upload*:{
  var %read
  sockread %read
  if (HTTP* 2* iswm %read) {
    echo 3 -a SUCCESS!
  }
  elseif (HTTP* 4* iswm %read) {
    echo 4 -a FAILED!
  }
}

on *:sockclose:upload*:{
  hfree $sockname
}


Link Copied to Clipboard