jabarin has asked for the wisdom of the Perl Monks concerning the following question:

I would like to upload a file without having to use a form with a file field (ie. the browse feature in IE). Rather I would like to make a request that submits the file automatically. What is the best way to do that?

Replies are listed 'Best First'.
Re: Uploading files using user::agent
by zentara (Cardinal) on Sep 22, 2006 at 12:36 UTC
    Well your question is kind of ambiguous. It sounds like you want the browser to upload automatically, but then you say user::agent, as in LWP::UserAgent?

    The browser can't do it.

    Here is how to use LWP::UserAgent. There are better scripts than this, which will also show upload progress, search for them.

    #!/usr/bin/perl use warnings; use strict; use HTTP::Request::Common qw(POST); use LWP::UserAgent; my $url= 'http://somesite.net/cgi-bin/uploader.cgi'; my $file = 'testout.tgz'; my $filezzzz = 'testzzzz.tgz'; my $ua = new LWP::UserAgent; my $req = POST $url, Content_Type => 'multipart/form-data', Content => [ p_upload => [ $file, $filezzzz ] #actual file, name to use as u +pload ]; my $res = $ua->request($req); if ( $res->is_success ) { print $res->as_string; } else { print $res->status_line; } exit 0;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Uploading files using user::agent
by Melly (Chaplain) on Sep 21, 2006 at 15:59 UTC

    I suspect that you can't - if you think about it, the security implications would be rather serious if a script could automatically start uploading a file...

    Tom Melly, tom@tomandlu.co.uk

      Err, just to clarify, by "a script" I mean "a script on the webserver"...

      If you want to upload a file to a server that you have, say, ftp access to, then it should be fairly straightforward. You should also be able to write a script that uploads a file to a website provided that you are running the script on your machine, not the website (using mechanise?)

      What you cannot do is write a script, place it on a server, and have that script automatically upload a file when someone accesses it.

      Tom Melly, tom@tomandlu.co.uk