Home

Search this site:
 

Horshack’s
very
own site

 

This is Bavaria, Germany!
You will find links to Perl,
bootdisks for system administration,
a lot of Linux,
Windows administration
and some personal belongings.
Uploading a file via HTTP with Perl

Uploading a file via HTTP to a Webserver

You need it for example to get pictures automatically every minute from a webcam and to send the files onto a webserver which is open to the public:

Serverpart of uploading a file

Download the cgi-serverpart as zip-file here, it works on Win- and Unixmachines and is configured via a small inifile in the same directory. This is the userinterface:

 


You can input MD5, filesize and a subdirectory of the maindirectory as additional information. This is for checking wether the file-upload was successful. This is the page after uploading a file:


Clientpart in Perl to automate an upload

HTTP-uploading a file is simply sending a few POST-variables to a webform

#!/usr/bin/perl
# file: uploadafile.pl
# call me like this:
# uploadafile.pl --url=http://www.x.com/cgi-bin/upload.pl --uploadfile=c:/temp/who.gif
#
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use Getopt::Long;
use File::Basename;

my ( $o_url, $o_fqn );
GetOptions(
    "url=s"        => \$o_url,
    "uploadfile=s" => \$o_fqn,
);

# mandatory arguments: url
&usage unless ( $o_url && $o_fqn );

my $url   = $o_url;
my $fname = $o_fqn;

# put timeouts, proxy etc into the useragent if needed
my $ua  = LWP::UserAgent->new();
my $req = POST $url, Content_Type => 'form-data',
	Content      => [
		submit => 1,
		upfile =>  [ $fname ]
	];
my $response = $ua->request($req);



if ($response->is_success()) {
    print "OK: ", $response->content;
} else {
    print $response->as_string;
}

exit;

sub usage {
	printf "usage: %s --url=%s --uploadfile=%s\\n",
		basename($0),'http://....','c:/data/myfile.jpg';
	exit
}
Impressum