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

I appologize for the long script below but you probably don't need to really look at anything in particular as this is a pretty general question. The script below uploads one file at a time, I was wondering how I would go about making four forms work at the same time so you could upload more than one file before reloading the page.

I was thinking I would to put each form and each upload process (setting filename, file location, transfer file, etc) in their own loop or subroutine but that seems pretty repetitive. Is there a simpler way to do this?

Thanks in advance.

my $mode = 0755; print header, start_html('upload form'); print "Upload formats allowed: jpg, gif, bmp.<br>"; print start_form( -method => 'post', -enctype => 'multipart/form-data' ), table( Tr( td("File: "), td( filefield( -name => 'upload', -size => 50, -maxlength => 80 ), ), ), Tr( td(), td( submit( 'button', 'submit' ), ) ) ), end_form(), hr; print "By clicking Submit you are agreeing that any legal discreptanci +es involved with the images you upload"; print " are not the responsibility of the designer of this script or t +he webhost. You are agreeing these"; print " pictures are not copyright material, do not contain viruses an +d does not promote sexual or violent"; print " activities. It is a legal signature of awknowledgement once y +ou click the Submit button.<br><br>"; if ( param() ) { # take form data my $remotefile = param('upload'); # make new variable to prevent overwriting of form data my $filename = $remotefile; # remove all directories in the file name path $filename =~ s/^.*[\\\/]//; # full file path to upload directory (must include filename) my $localfile = ""; # full url to upload directory (cannot include filename or an end +slash /) my $url = ""; my $type = uploadInfo($remotefile)->{'Content-Type'}; unless ( $type eq 'image/pjpeg' || $type eq 'image/gif' || $type e +q 'image/bmp') { print "Wrong! This is not a supported file type."; exit; } # print "type: $type <br><br>"; # open a new file and transfer bit by bit from what's in the buffe +r open( SAVED, ">>$localfile" ); # || die $!; while ( $bytesread = read( $remotefile, $buffer, 1024 ) ) { print SAVED $buffer; } close SAVED; chmod $mode, "$localfile"; # or die "can't chmod: $!"; print "-----------------------------<br>"; print qq(File was uploaded to <a href="$url\/$filename">$url\/$filename</a>) +; # required since module was not preinstalled on server # use lib ""; use Image::Info qw(image_info dim); # assigning info to a filename (better be an image) my $info = image_info("$localfile"); # if for any reason we can't open the file, this error trap should +pick it up if ( my $error = $info->{error} ) { #die "Can't parse image info: $error\n"; } # unommit next line if you want to use/post the image's color #my $color = $info->{color_type}; # declaring the width and heighth of your image my ( $w, $h ) = dim($info); print "<br><br><br>"; print "Image code:<br>"; print qq(&lt;p style =\"background:url\($url\/$filename\)\;width:$w\;height: +$h\;\"&gt;); print "<br>-----------------------------<br>"; }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

20030901 Edit by jeffa: Changed title from 'Simultaneus(sp?) uploads '

Replies are listed 'Best First'.
Re: Simultaneous uploads
by tcf22 (Priest) on Aug 31, 2003 at 23:15 UTC
    You can't submit multiple forms at once, but you can just have four <input type="file"...> fields.
      What he said. Have as many FILE fields as you like but the browser and/or server may choke if you ask too much of either.

      The multiple-field approach is the approach taken by PSPUpload, a shareware script, which I've found works well. It has quite a few useful features as well.

      And it's spelled "simultaneous", but it won't be simultaneous, it'll just be consecutive, without you having to submit more than one form.



      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      I'm not sure... you might be able to with Java Script, but it's ugly... Have each form submit individually on a single submit, and open a new window for each form... (shudder).

      ----
      Zak
Re: Simultaneous uploads
by bobn (Chaplain) on Sep 01, 2003 at 05:37 UTC

    Here's fragments of something I did along these lines:

    for ( 1..$MAXNUM) { my $out_key = radio_group(-name=>"key$_", -value=>\@in_keys, -rows +=>1, -default=>0); $out_key =~ s!<td>!<td>Choose Key:</td><td>!i; $out_key =~ s!</td>!</td>\n!gi; my $out_scale = 'Scale/Chord type: ' . popup_menu(-name=>"scale$_" +, -value=>\@in_scales, -length=>scalar(@in_scales) + 3, -default=>'ma +jor,'); for ($out_key, $out_scale) { s/<TABLE/<TABLE border='1'/i; print; +} } # later # if ( param() ) { # stuff for ( 1..$MAXNUM ) { if ( param("key$_") and param("scale$_") ) { push @cmd, "-s", param("key$_") ."," . param("scale$_" +); } } # other stuff }
    This runs under strict, too.

    This can be seen in action at http://bob-n.com/cgi-bin/chord.pl, downloaded in full at: http://bob-n.com/comp/scripts/index.html

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

Re: Simultaneous uploads
by bradcathey (Prior) on Sep 01, 2003 at 14:00 UTC
    Not completely sure I understand the question, but if I'm interpreting correctly, you want to allow the user to select several files and only submit them once.

    I have done this several times: in my HTML, have a series of <input type=file> boxes, named "file1" "file2" etc., with the expected "Submit" underneath. Then in my Perl, I loop the names through my uploader.

    What I would really like to do is have the user select a folder on their local drive and have the Perl grep all the files, uploading them as they are read, or through an array. Have tried it and can't seem to land on the right code, especially on my Mac (weird path names).

    Hope this helps and that I got close.