Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: CGI, uploading multiple files

by huck (Prior)
on Mar 14, 2017 at 01:07 UTC ( #1184512=note: print w/replies, xml ) Need Help??


in reply to CGI, uploading multiple files

Well i found out what was in the parts, PERLIO filehandles

and i found the MAGIC SPELL http://stackoverflow.com/questions/3196783/perl-file-upload-cant-init-filehandle File upload forms need to specify enctype="multipart/form-data". ----- Sinan Ünür

Ubuntu tested code (well at least with $tostdout=1).

AND Notice how the filename starts with a folder!

#!/usr/bin/perl use strict; use warnings; use CGI; use Data::Dumper; use HTML::Entities qw/encode_entities/; my $q = CGI->new(); print $q->header; unless ($q->param('multi_files')) { print ' <html> <head> <title>Stupid Test Site</title> </head> <body> <h1>This is a test</h1> <form action="multiupload.pl" enctype="multipart/form-data". +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> '; exit; } 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 $tostdout=1; ###### see ###### http://stackoverflow.com/questions/3196783/perl-file-upload-can +t-init-filehandle ###### File upload forms need to specify enctype="multipart/form-data" +. ----- Sinan Ünür my @io_handles=$q->upload('multi_files'); print '<pre>'; print Dumper(\@files); print Dumper(\@io_handles); print '</pre>'; print '<br>step0'; for my $upload (@files){ print "<br>Upload this please -- $upload<br>"; if ($upload){ eval { if ($tostdout) { print '<pre>' ; while ( my $buffer = <$upload>) { print STDOUT encode_entities($buffer); } # while print '</pre>' ; } # tostdout else { open (OUTFILE,">$upload_folder/$upload") or die $!;; binmode OUTFILE; while ( my $buffer = <$upload>) { print OUTFILE $buffer; } # while close OUTFILE; } # notstdout }; # eval print 'error:'.$@.'<br>' if $@; } # upload else { print "<br><b>Guess it's broken</b><br/>"; } # else } # $upload print "<p><b>Name</b> -- $name<br><b>Email</b> -- $email<br><b>Comment +s</b> -- $comments<br>"; print $q->end_html;
Result
$VAR1 = [ bless( \*{'Fh::fh00001multiuploads/a.txt'}, 'Fh' ), bless( \*{'Fh::fh00002multiuploads/Copy of a.txt'}, 'Fh' ), bless( \*{'Fh::fh00003multiuploads/Copy (2) of a.txt'}, 'Fh' + ) ]; $VAR1 = [ bless( \*{'Fh::fh00001multiuploads/a.txt'}, 'Fh' ), bless( \*{'Fh::fh00002multiuploads/Copy of a.txt'}, 'Fh' ), bless( \*{'Fh::fh00003multiuploads/Copy (2) of a.txt'}, 'Fh' + ) ]; step0 Upload this please -- multiuploads/a.txt this is a.txt Upload this please -- multiuploads/Copy of a.txt this is a.txt Upload this please -- multiuploads/Copy (2) of a.txt this is a.txt Name -- a Email -- b Comments -- ok

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2021-10-25 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?