Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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="// +ery.min.js" ></script> </head> <body> <h1>This is a test</h1> <form action="/cgi-enabled/" 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, 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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2021-10-27 16:16 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (94 votes). Check out past polls.