upload multiple files with perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djia002
    New Member
    • Aug 2010
    • 2

    upload multiple files with perl

    Hello everyone,

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

    My Html is this,

    Code:
    <form enctype="multipart/form-data" method=post action="/cgi-bin/upload/mfile.pl">
    <input type=file name=file size="30"><br />
    <input type=file name=file size="30"><br />
    <input type=file name=file size="30"><br />
    <input type="submit" value="Upload Image!">
    </form>
    My perl

    Code:
    my @upload_filehandles = $query ->upload('file'); 
    my $arrLen = scalar @upload_filehandles; 
    logit("The size of array is $arrLen");
    I am expecting a array of 3 upload_filehand les, but the size of array is 1 only when it print out

    How can i upload multiple files with same named field?


    Many thanks
    Last edited by numberwhun; Aug 5 '10, 02:33 PM. Reason: Please Use CODE TAGS!
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    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

    Comment

    • djia002
      New Member
      • Aug 2010
      • 2

      #3
      Thanks chaarmann,

      there is link at http://bytes.com/topic/perl/answers/...les-upload-cgi talking about using same file name for multiple files upload. it seems like that is possible. but it just doesn't work for me.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        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 http://www.planet-source-code.com/UR...!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.

        Comment

        Working...