Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload file and send via Http post

Status
Not open for further replies.

pwils025

Programmer
Oct 7, 2003
45
US
How do I handle a file via html form with my perl script, so that I can post that file to a server?

I have an html form which requests a user to select a file for upload. I then post that form to a cgi page, which pulls out the data in name/value pairs using CGI.pm. I then take those pairs and post them to the server.

I do it this way, so that I can handle the xml response sent back from the server. My problem is that I do not know how to post files (gif, jpg, doc, etc).

Does anyone have any code showing how this is done. I guess using CGI::Simple or something?

Thanks!



Paul
 
Still don't quite understand the upload process. I need an explanation of how this transpires. First the html form uploads the file to a local directory, then using "$ua->request(POST..." I post that locally stored file to the remote server?

The problem is that I'm not sure how this file handling is taken place.

Thanks.
 
Here's some code that might help you. This doesn't use CGI::Simple, but it does allow for file uploads quite nicely.

Code:
my $outputdir = '/path/to/dir';
my $file #this should be the value for the input type of file in your HTML file
my ($total_size, $size, $data);

open(SAVE, ">$outputdir/$file") or die "Can't open file: $!";

binmode SAVE;

while($size = read($file, $data, 1024))
{
    print SAVE $data;
    $total_size += $size;
}

close(SAVE);

- Rieekan
 
I'm sorry, let me rephrase my question.

Using LWP::UserAgent or something similar, how do I post a file from my script? Again, I have an html form, which posts to a perl cgi and I need that cgi to re-post to a server, which I don't manage (remote).

Here is my code for handling the other fields in the html form:

$file_name = $upload->file_name($fileName);
$file_type = $upload->file_type($fileName);

$ua = LWP::UserAgent->new;
my $res = $ua->request(POST ' Content_Type => 'form-data',
Content => [$headers[0] => $content[0], $headers[1] => $content[1],
$headers[2] => $content[2], $headers[3] => $content[3], $headers[4] => $content[4],
$headers[5] => $content[5], $headers[6] => $content[6], $headers[7] => $content[7],
$headers[8] => $content[8], $headers[9] => $content[9], $headers[10] => $content[10],
$headers[14] => $content[14], $headers[15] => $content[15], $headers[16] => $content[16],
$headers[17] => $content[17], $headers[18] => $content[18], $headers[19] => $content[19],
$headers[20] => $content[20], $headers[21] => $content[21], $headers[22] => $content[22],
$headers[23] => $content[23], $headers[24] => $content[24], $headers[25] => $content[25],
$headers[26] => $content[26], $headers[27] => $content[27], $headers[28] => $content[28],
$headers[29] => $content[29], $file_name => $fileVal]);


Thanks everyone!
 
Thanks for the help, but here's the error I keep getting:

Can't open file c:\images\sample.gif: No such file or directory at /usr/local/apache2/sites/marvel.com/htdocs/cgi-bin/response2.pl
 
Thanks everyone for your help. I put together a modification of what I was sent and here is my final answer:

$upload_dir = "/usr/local/apache2/sites/marvel.com/htdocs/cgi-bin/dev/";
$fileName = $cgi->param('file01');
@file = split(/\\/,$fileName);
$file1 = "";
$file1 = pop(@file);
$upload_filehandle = $cgi->upload('file01');

open FH, ">$upload_dir/$file1";
binmode FH;
while ( <$upload_filehandle> )
{
print FH;
}
close FH;

Very close to Rieekan's example.

Thanks again. I now upload the file to my server and then grab it so I can send it in a http post via user agent (grrrr). Not my ideal way of doing it, but it gets the job done. I then delete the file so my directory stays clean.
 
The link I posted above should give you a start on how to use LWP instead of a user agent to upload the file from your server to another.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top