Uploading 2 files with 1 CGI buffer

W

William

Code in question:
#!/usr/bin/perl -w

use strict;

my $tempfile;
my $upath;
my $savedfilename;

# The following reads in the CGI buffer, and writes it to a temporary file
# which will used later.
read ( STDIN, my $buffer, $ENV{'CONTENT_LENGTH'} );
open ( TEMP_FD,">$tempfile" );
print TEMP_FD $buffer;
close ( TEMP_FD );

open ( TEMPFILE_FD, $tempfile );

# Gotta pull the MIME/multipart separator line out
$_ = <TEMPFILE_FD>; (
my $vernum) = /(\d+)/;

$_ = <TEMPFILE_FD>;
my $filetemp = $1 if (/filename=\"(.*)\"/);

#create the output file in the upload directory
open ( OUTFILE_FD, ">$upath$savedfilename");

close ( TEMPFILE_FD );
close ( OUTFILE_FD );


Question: my above code only uploads 1 file. How do I upload 2 files
simulataenously with the same CGI buffer?

P.S. this script is run from a website with 2 "Browse..." for file
widgets.
 
X

xhoster

William said:
Code in question: ....

Question: my above code only uploads 1 file. How do I upload 2 files
simulataenously with the same CGI buffer?

I would use the module CGI.pm to do it. If I couldn't use CGI.pm for some
bizarre reason, then I would look at the source code for CGI.pm, mostly in
the subroutine "read_multipart", and use that as a starting point.

Xho
 
G

Gunnar Hjalmarsson

I would use the module CGI.pm to do it.

And I would use CGI::UploadEasy (which, in turn, makes use of CGI.pm).

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI::UploadEasy;
use Data::Dumper;

my $uploaddir = '/path/to/upload/directory';
my $ue = CGI::UploadEasy->new(-uploaddir => $uploaddir);

# At this point all the files are saved in $uploaddir.
# For a raw printout of related info, you can do:

my $info = $ue->fileinfo;
my $cgi = $ue->cgiobject;
print $cgi->header('text/plain');
print Dumper $info;
__END__
 
T

Tintin

William said:
Code in question:
#!/usr/bin/perl -w

use strict;

my $tempfile;
my $upath;
my $savedfilename;

# The following reads in the CGI buffer, and writes it to a temporary file
# which will used later.
read ( STDIN, my $buffer, $ENV{'CONTENT_LENGTH'} );
open ( TEMP_FD,">$tempfile" );

$tempfile is not set to anything here.
print TEMP_FD $buffer;
close ( TEMP_FD );

open ( TEMPFILE_FD, $tempfile );

# Gotta pull the MIME/multipart separator line out
$_ = <TEMPFILE_FD>; (
my $vernum) = /(\d+)/;

$_ = <TEMPFILE_FD>;
my $filetemp = $1 if (/filename=\"(.*)\"/);

#create the output file in the upload directory
open ( OUTFILE_FD, ">$upath$savedfilename");

$upath and $savedfilename are not set to anything either.
close ( TEMPFILE_FD );
close ( OUTFILE_FD );

Your above code could never have "worked".
 
W

William

I would use the module CGI.pm to do it. If I couldn't use CGI.pm for some
bizarre reason, then I would look at the source code for CGI.pm, mostly in
the subroutine "read_multipart", and use that as a starting point.

I looked at perldoc -m CGI and found the method read_multipart. I have to
concede that I have difficulty understanding the ~150 lines of code as I
plowed through them.

I would appreciate an example of reading multiple files using
read_multipart, which I could not found on google.

The following sample script can be used as a basis for the example:

#!/usr/bin/perl -w

use strict;

my $tempfile = "temp.txt";
my $savedfilename = "trs_vol.txt";

# The following reads in the CGI buffer, and writes it to a temporary file
# which will used later.
read ( STDIN, my $buffer, $ENV{'CONTENT_LENGTH'} );
open ( TEMP_FD,">$tempfile" );
print TEMP_FD $buffer;
close ( TEMP_FD );

open ( TEMPFILE_FD, $tempfile );

# Gotta pull the MIME/multipart separator line out
$_ = <TEMPFILE_FD>; (
my $vernum) = /(\d+)/;

$_ = <TEMPFILE_FD>;
my $filetemp = $1 if (/filename=\"(.*)\"/);

#create the output file in the upload directory
open ( OUTFILE_FD, ">$savedfilename");

close ( TEMPFILE_FD );
close ( OUTFILE_FD );
 
W

William

And I would use CGI::UploadEasy (which, in turn, makes use of CGI.pm).

I tried "use CGI::UploadEasy" as follows:
use CGI::UploadEasy;


but I got the following error message:
perl -c test2.pl
Can't locate CGI/UploadEasy.pm in @INC (@INC contains:
/usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
/usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at
test2.pl line 4.
BEGIN failed--compilation aborted at test2.pl line 4.
 
P

Paul Lalli

William said:
I tried "use CGI::UploadEasy" as follows:
use CGI::UploadEasy;


but I got the following error message:
perl -c test2.pl
Can't locate CGI/UploadEasy.pm in @INC (@INC contains:
/usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
/usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at
test2.pl line 4.
BEGIN failed--compilation aborted at test2.pl line 4.

CGI::UploadEasy is not a core module. You need to download it from
CPAN:

perldoc perlmodlib
perldoc -q library
http://search.cpan.org/~gunnar/CGI-UploadEasy-0.11/UploadEasy.pm

Paul Lalli
 
P

Paul Lalli

William said:
I downloaded CGI::UploadEasy to my HD, but I could not FTP the
CGI::UploadEasy module to any of the directories in my @INC, since I do
not have root access.

I have no idea what makes you think that was the proper procedure to
begin with.
How do I complete my installation of CGI::UploadEasy?

Did you read any of those links I posted, or did you just grab the .pm
file and just try to put it in an @INC directory?

Read those two perldocs. If for some reason you can't or don't want to
read the information at your command line, the same documents are
available online at:
http://perldoc.perl.org/perlmodlib.html
http://perldoc.perl.org/perlfaq8.html#How-do-I-keep-my-own-module/library-directory?

Paul Lalli
 
G

Gunnar Hjalmarsson

William said:
I downloaded CGI::UploadEasy to my HD, but I could not FTP the
CGI::UploadEasy module to any of the directories in my @INC, since I do
not have root access.

How do I complete my installation of CGI::UploadEasy?

Paul's suggestions are preferrable if you have shell access. Otherwise,
since CGI::UploadEasy is a pure Perl module, it's also possible to
simply upload CGI::UploadEasy.pm using FTP to a directory you do have
access to.

For instance, you can create this subdirectory under your cgi-bin:

/path/to/cgi-bin/perllib/CGI

Then in your script, before loading CGI::UploadEasy, you add:

use lib '/path/to/cgi-bin/perllib';

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top