472,961 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

Handle Multiple files upload in CGI

109 100+
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
6 13128
KevinADC
4,059 Expert 2GB
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
skyy
109 100+
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
4,059 Expert 2GB
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
skyy
109 100+
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
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
3,509 Expert Mod 2GB
Next time, legolas188, please surround your code in code tags so we don't have to.

Regards,

Jeff
Jul 1 '08 #7

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

Similar topics

10
by: 3A Web Hosting | last post by:
Hi Is it possible to perform multiple file uploads via a form? It's no problem uploading single files but I want to be able to highlight a group of files and upload them all in one go. My...
6
by: x. zhang | last post by:
Hi Guys, We know that we can use <input type=file ...> to upload one file per time to the server. My question is if there are some way to upload multiple files per time to the server. (Of...
2
by: Sundar | last post by:
Hi, I am working on ASP.Net. My requirement is that I want to Upload Multiple Files to the Server. I need to have ONLY ONE FILE UPLOAD CONTROL in my page. I SHOULD NOT SUBMIT THE PAGE FOR EACH...
5
by: Jason | last post by:
I have a potential need to upload multiple PDF legal documents. Is it possible to attach more than one file per upload?
7
by: crowl | last post by:
Hi there, I am looking for a component allowing me uploading multiple files by my asp page. I have found several components achieving this by require a <input type="file" name="FileX"> field for...
0
by: dann2 | last post by:
hello, i try to upload in an access db two pictures at the same time. i use the adjusted sample code from persits. it looks like this: ... '<% ' Create an instance of AspUpload object 'Set...
7
by: der_grobi | last post by:
That is the Problem: I have an ASP.NET Webapplicatipon where I can upload single files to the Server. That works fine. But now, I want to Upload multiple files. I know the path of the files, i...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.