WGET CGI Post

From Noah.org
Revision as of 10:01, 20 December 2006 by Root (talk | contribs)
Jump to navigationJump to search

Post upload using wget

I like to use wget in shell scripts to upload files over http to remote servers. I use this in situations where I cannot use scp or ftp.

This requires a little python script on the client side. The script is to urlencode the file to be uploaded. It's a very simple script, so you can easily replace it with the language of your choice.

Step 1. On the client side create a Python script called "postencode.py":

    #!/bin/env python
    import sys, urllib, base64
    input_filename = sys.argv[1]
    postwad_filename = input_filename + ".post"
    datawad = base64.encodestring(file(input_filename, "rb").read())
    postwad = urllib.urlencode({"filedata":datawad, "filename":input_filename})
    file(postwad_filename, "wb").write(postwad)
    print postwad_filename

Step 2. On the server side create a PHP create a script called "upload.php":

    <html><head><title>Post Upload Tool</title><body>
    <?php
        $filename = $_POST['filename'];
        $filedata = base64_decode($_POST['filedata']);
        echo "<h1>$filename</h1>";
        $fout = fopen($filename,"wb");
        fwrite($fout, $filedata);
        fclose($fout);
    ?>
    </body></html>

Step 3. Use wget to post a file to the server (note the backticks):

    wget --post-file=`./postencode.py FILENAME` http://www.example.com/upload.php

If you choose not to use Python for the postencode script then here are references for other languages.

In Perl use:

   use URI::Escape;
   uri_escape(...);

In PHP use:

   urlencode(...);