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

upload multiple files with perl

P: 2
Hello everyone,

I am new to Perl, and stuck with file uploading issue.

My Html is this,

Expand|Select|Wrap|Line Numbers
  1. <form enctype="multipart/form-data" method=post action="/cgi-bin/upload/">
  2. <input type=file name=file size="30"><br />
  3. <input type=file name=file size="30"><br />
  4. <input type=file name=file size="30"><br />
  5. <input type="submit" value="Upload Image!">
  6. </form>
My perl

Expand|Select|Wrap|Line Numbers
  1. my @upload_filehandles = $query ->upload('file'); 
  2. my $arrLen = scalar @upload_filehandles; 
  3. logit("The size of array is $arrLen");
I am expecting a array of 3 upload_filehandles, but the size of array is 1 only when it print out

How can i upload multiple files with same named field?

Many thanks
Aug 3 '10 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 785
If you upload a file then you can't access the data the same way you access the values of option-boxes or text-boxes or URL-parameters. You must handle multipart content and you will get binary data that you will need to read from the input stream byte by byte yourself through looping.

Second, if you use the same name or id on html-elements multiple times, you will either get an array of their values back, or only the first value. Even if you get an array with filenames back, you can't determine which binary data inside the input stream then corresponds to what name.

So uploading many files at once doesn't work the way you want it.
Either stick with it and change your interface to the way goggle-mail is uploading attachments. (Here you can only upload files one by one, but then returning back to the webpage where it shows a list of uploaded files so far before committing the mail).

Or maybe this trick will help:
Put many forms inside one webpage. Each form has a single file-input box. Then make a button that, when pressed, calls a javascript to executes form.submit() on all forms. But I fear you may not like it in this way when there are opening many windows to ask for selecting the files one by one
Aug 3 '10 #2

P: 2
Thanks chaarmann,

there is link at talking about using same file name for multiple files upload. it seems like that is possible. but it just doesn't work for me.
Aug 3 '10 #3

Expert 100+
P: 785
Dear Dija002,

I read the link. But the Autor "KevinADC" does not tell us why you should use the same file name. But if you look at the fileUpload-HTML form of another solution that works fine (see!6/anyname.htm), they are using different names! (So maybe it's a browser compatibility issue???)

By the way, I analyzed the last source code given by "legolas188" in the thread you mentioned. There he is getting only the file names as an array and then trying to read these files from disk (and not from the browser's data stream). There must be other code which does the upload of the file but which is not listed here. So no wonder it doesn't work with you.

I suggest that you try the code mentioned in the link above. I analyzed it and it seems to work (although it's very cryptical and needs a rewrite. It's missing meaningful variable names and comments inside). I haven't tried it yet, but I couldn't see any error and it looks the same way I have done it in C++ once, parsing the input stream from the browser.
Aug 4 '10 #4

Post your reply

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