Uploading 2 files with 1 CGI buffer

Discussion in 'Perl Misc' started by William, Jan 9, 2006.

  1. William

    William Guest

    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.
    William, Jan 9, 2006
    #1
    1. Advertising

  2. William

    Guest

    William <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jan 9, 2006
    #2
    1. Advertising

  3. wrote:
    > William <> wrote:
    >>How do I upload 2 files simulataenously with the same CGI buffer?

    >
    > 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__

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 9, 2006
    #3
  4. William

    Tintin Guest

    "William" <> wrote in message
    news:p...
    >
    > 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".
    Tintin, Jan 10, 2006
    #4
  5. William

    William Guest

    On Mon, 9 Jan 2006, wrote:

    > William <> wrote:
    >> 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.


    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 );
    William, Jan 10, 2006
    #5
  6. William

    William Guest

    On Mon, 9 Jan 2006, Gunnar Hjalmarsson wrote:

    > wrote:
    >> William <> wrote:
    >>> How do I upload 2 files simulataenously with the same CGI buffer?

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

    >
    > 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.


    > > #!/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__
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >
    William, Jan 10, 2006
    #6
  7. William

    Paul Lalli Guest

    William wrote:
    > On Mon, 9 Jan 2006, Gunnar Hjalmarsson wrote:
    >
    > >
    > > 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.


    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
    Paul Lalli, Jan 10, 2006
    #7
  8. William

    William Guest

    On Tue, 10 Jan 2006, Paul Lalli wrote:

    > William wrote:
    >> On Mon, 9 Jan 2006, Gunnar Hjalmarsson wrote:
    >>
    >>>
    >>> 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.

    >
    > 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
    >


    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?
    William, Jan 10, 2006
    #8
  9. William

    Paul Lalli Guest

    William wrote:
    > On Tue, 10 Jan 2006, Paul Lalli wrote:
    > > 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

    >
    > 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
    Paul Lalli, Jan 10, 2006
    #9
  10. William

    Paul Lalli Guest

    Paul Lalli, Jan 10, 2006
    #10
  11. William wrote:
    > 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 10, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dermoon
    Replies:
    0
    Views:
    462
    dermoon
    Oct 8, 2003
  2. D Borland

    Uploading files using CGI.pm

    D Borland, Sep 28, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    174
    Alan J. Flavell
    Sep 29, 2003
  3. darius

    maybe OT: uploading files w/ cgi

    darius, Jul 21, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    123
    darius
    Jul 21, 2004
  4. vb.net cgi uploading files

    , Nov 8, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    438
    A. Sinan Unur
    Nov 8, 2005
  5. ccc31807

    CGI and temFileName -- uploading files

    ccc31807, Dec 8, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    240
    ccc31807
    Dec 8, 2011
Loading...

Share This Page