I've seen this question asked a few times and yet I haven't found a solution so I am going to ask again (specific to my question).

I have a simple web form in which I want the user to select a folder to upload (I only care about the contents of the folder). So my attempt to make this work is as followed:

#!/usr/bin/perl use strict; use warnings; use CGI; use Data::Dumper; my $q = CGI->new(); print $q->header; my $upload_folder = '/var/www/html/uploads'; my $name = $q->param('name'); my $email = $q->param('email'); my $comments = $q->param('comments'); my @files = $q->param('multi_files'); foreach my $upload(@files){ print "Upload this please -- $upload<br>"; my $upload_file = $q->upload($upload); if ($upload_file){ open (OUTFILE,">$upload_folder/$upload") or die $!;; binmode OUTFILE; while (<$upload_file>) { print OUTFILE; } } else { print "<b>Guess it's broken</b><br/>"; } } print "<p><b>Name</b> -- $name<br><b>Email</b> -- $email<br><b>Comment +s</b> -- $comments<br>"; print $q->end_html;
When I run the script all the parameters are correct, the files print out as expected but when I execute the upload query it returns blank. This is my first attempt at using CGI as I tend to use other languages to process forms.

Here is the code from the form just in case :

<html> <head> <title>Stupid Test Site</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jqu +ery.min.js" ></script> </head> <body> <h1>This is a test</h1> <form action="/cgi-enabled/test.pl" method="POST"> Your Name: <input type="text" name="name"><br> Email Address: <input type="text" name="email"><br> Select PO folder: <input name="multi_files" type="file" we +bkitdirectory multiple /><br/> Comments:<br> <textarea name="comments" rows="5" cols="60"></textarea><b +r> <input type="submit" value="Send"> </form> </body> </html>
Any help on this issue would be greatly appreciated.

I've also posted this question on stackoverflow.com, sorry for the cross post --


----- Update -----


My error was in fact due to the missing enctype="multipart/form-data" in the form. I made some changes and tweaked some things based on all of you input. Here is my final working code :
#!/usr/bin/perl use strict; use warnings; use CGI; use Data::Dumper; use CGI::Carp qw( fatalsToBrowser ); use HTML::Entities qw/encode_entities/; use File::Copy qw' copy '; use File::Basename; my $q = new CGI; print $q->header; my $upload_folder = '/var/www/html/uploads'; my $name = $q->param('name'); my $email = $q->param('email'); my $comments = $q->param('comments'); my @files = $q->param('multi_files'); my @io_handles=$q->upload('multi_files'); my %file_hash; foreach my $item(@files){ next unless $item =~ /.+\/(M.+\.pdf)/; foreach my $sub_item(@io_handles){ if($item eq $sub_item){ $file_hash{$item} = $sub_item; } } } chdir $upload_folder or die "Cannot chdir to upload destination direct +ory: $!\n"; print '<ul>'; foreach my $key(keys %file_hash){ my $base = basename($file_hash{$key});# my $tmpfilename = $q->tmpFileName($file_hash{$key}); my $destFile = File::Spec->catfile($upload_folder,$base); copy( $tmpfilename, $destFile ) or die "Copy to ($destFile) failed +: $!\n"; print '<li>Sucessfully uploaded --- <b>', CGI->escapeHTML(basename +($key)), '</b></li>'; } print '</ul>'; print "<p><b>Name</b> -- $name<br><b>Email</b> -- $email<br><b>Comment +s</b> -- $comments<br>"; print $q->end_html;
So far so good. Thanks again for all the help. Will be posting my solution on Stackoverflow as well to close that out.

In reply to CGI, uploading multiple files by edimusrex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.