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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload client file to server via browser

Status
Not open for further replies.

thomgreen

IS-IT--Management
Sep 4, 2002
56
US
I do not work a ton with Perl but enough to accomplish small tasks. I have hit a wall with my current task. I want to upload a file from the users local drive to the server but... I also have drop-down & text boxes with data that needs to be parsed out in the perl script.

I can upload the file if I include 'ENCTYPE="multipart/form-data"' in the <FORM> tab but, I am having trouble parsing the data. If I take out the 'ENCTYPE="multipart/form-data"' then I can parse the data fine but my file will not upload.

Does anyone have code that will allow me to both upload the file and parse out the data sent allong in the HTML POST transmission?
 
use CGI;

The HTTP request is different when a file is being uploaded than it is when a normal request is sent. The CGI module handles both cases. Just use it instead of trying to roll your own code, because this is one of many ways in which HTTP requests can drastically change depending on the context, and you'll just give yourself a lot of headaches trying to program your own codes. Plus, CGI has been around for a while and is pretty secure and covers all the little gray areas some newbie programmers overlook.


-------------
Cuvou.com | The NEW Kirsle.net
 
Code:
#!/usr/bin/perl

use strict;

my $name = param('name');    # by field
my $file = upload('file');   # or file

open (OUTFILE, "/path/to/directory/$name") or die $!;
while ( <$file> ) {
    print OUTFILE $_;
}
close(OUTFILE);

M. Brooks
 
I totally spaced on my last post. This is the correct way.
Code:
#!/usr/bin/perl

use strict;
[b]use CGI;

my $q = new CGI;[/b]

my $name = [b]$q->[/b]param('name');    # by field
my $file = [b]$q->[/b]upload('file');   # or file

open (OUTFILE, "/path/to/directory/$name") or die $!;
while ( <$file> ) {
    print OUTFILE $_;
}
close(OUTFILE);

M. Brooks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top