DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple FileFactory File Upload Tool

07.30.2008
| 7005 views |
  • submit to reddit
        
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;

if ($#ARGV == -1)
{
        print "syntax: $0 <filename>\n";
        exit();
}

my $ua = LWP::UserAgent->new;
$ua->timeout(15);
$ua->requests_redirectable(['GET','POST']);
$ua->max_redirect(1);

my $response = $ua->get('http://www.filefactory.com/');
catch_unsuccess($response);
if ($response->content !~ /<form id="uploader" action="(http:\/\/.+?)" method="post" enctype="multipart\/form-data">/)
{
        print STDERR "unable to initiate upload\n";
        exit(1);
}

$response = $ua->post($1, ['redirect' => '1', 'file' => [$ARGV[0]]], 'content_type' => 'multipart/form-data');
catch_unsuccess($response);
if ($response->content =~ /<a style="color:green" href="(http:\/\/www\.filefactory\.com\/.+?)">/)
{
        printf("upload successful!\n%s\n", $1);
}

sub catch_unsuccess
{
        my ($response) = @_;
        if (!$response->is_success)
        {
                print STDERR $response->status_line . "\n";
                exit(1);
        }
}