By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

Handle Multiple files upload in CGI

100+
P: 109
Hi.. i would like to know how to handle multiple upload files with the cgi module in perl script..

With single file,

Expand|Select|Wrap|Line Numbers
  1. my $my_cgi new CGI;
  2. my $upload_filehandle = cgi ->upload('file1');
  3. my $filename = $cgi->param('file1');
  4. $filename =~ s/.*[\/\\](.*)/$1/;
  5. if ($upload_filehandle) {
  6.     open(FILE1, ">$upload_dir/$filename") || print("Could not open file!");
  7.     binmode FILE1;
  8.     while (<$upload_filehandle>) {
  9.         print FILE1 ;
  10.     }
  11.     close FILE1;
  12.  
For multiple files, how to determine how many files are there? if the filename are file1, file2, file3, file4......
May 23 '07 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
Don't use different names for the file upload fields, use the same name for all of them. Then read in the value of the file fields in list context (array) instead of scalar context:

my @upload_filehandles = cgi ->upload('fileup');
May 23 '07 #2

100+
P: 109
Don't use different names for the file upload fields, use the same name for all of them. Then read in the value of the file fields in list context (array) instead of scalar context:

my @upload_filehandles = cgi ->upload('fileup');
Hi.. thanks for the reply..

I know that option is available.. but i do not want to have the same filename cos i want to carry out some operations later.
May 23 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
You should still be able to do anything you wanted to even if the file fields are all the same name. But if you wanted to use different names you could hard code them into the script if you know what the names will be.

my @file_upload_fields = qw(file1 file2 file3 file4 file5);
foreach my $fields (@file_upload_fields) {
....
}
May 23 '07 #4

100+
P: 109
You should still be able to do anything you wanted to even if the file fields are all the same name. But if you wanted to use different names you could hard code them into the script if you know what the names will be.

my @file_upload_fields = qw(file1 file2 file3 file4 file5);
foreach my $fields (@file_upload_fields) {
....
}
Hi thanks for the reply...

Anyway i managed to solve the problem using some loops and checking.. thanks
May 23 '07 #5

P: 1
Dear friends,

Expand|Select|Wrap|Line Numbers
  1.   my $respid = $q->param('rid');
  2.   my ($data, $n, $size);
  3.   if ($q->param('cmd') eq 'upload_img'){
  4.     my @file = ();
  5.     my @files = $q->param('file');
  6.  
  7.     foreach (@files){
  8.       push @file, $_ if  length $_ > 0;
  9.     }
  10.  
  11.     my $r = ();
  12.     foreach my $f (@file){
  13.       while (($n = read $f, $data, 10000000, $size) != 0) {
  14.     $r = $data;
  15.       }
  16.  
  17.   # create record in resp_images to have an unique image id
  18.   my $query = "INSERT INTO IMAGES2 (RespondentID, UserID, Created, Completed)".
  19.               "VALUES ($respid, '1', NOW(), 1)";
  20.   # insert 
  21.   $dbh->do ($query);
  22.  
  23.   # set imageid into cookie message
  24.   my $image_id = $dbh->{'mysql_insertid'};
  25.  
  26.  if ($image_id) {
  27.   my $query = "UPDATE ".IMAGES2.
  28.               " SET Image=".$dbh->quote($r)." WHERE ImageID=".$image_id;
  29.   $dbh->do($query);
  30.  
  31. }
  32.  
  33.   }
  34.  
  35.     }
  36.  
  37.     print $q->startform(-name   => 'form',
  38.             -method => 'POST',
  39.             -action => 'upload_img',
  40.             -enctype => 'multipart/form-data'
  41.                );
  42.     print "<table border=0 style=\"color:black;\" valign=top cellspacing=0 cellpadding=0 width=35%>";
  43.     print "<tr>";
  44.     print "<td style=\"color:black;\"  colspan=3 height=\"70\">";
  45.     for (my $i=0; $i<5; $i++){
  46.       print " Resim Seç : </b><input type=\"file\" name=\"file\" /><br><br>";
  47.     }
  48.     print $q->button
  49.       (-name    => 'update',
  50.        -value   => "Gönder",
  51.        -style   => $RS{BUTTON_STYLE}."width:75;",
  52.        -onClick => "cmd.value='upload_img';this.form.submit();"
  53.       );
  54.     print "</center></td></tr>";
  55.     print &hidden_param('cmd'  , '');
  56.     print &hidden_param('rid'  , $q->param('rid'));
  57.     print $q->endform;
  58.  
see you soon...
Jul 1 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
Next time, legolas188, please surround your code in code tags so we don't have to.

Regards,

Jeff
Jul 1 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.