arun_amiable has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
We are trying to upload multiple files on the server. For this we are reading filenames from a text file and uploading those files in a loop' but below mentioned code is not working please help

#!/usr/bin/perl use CGI qw(:standard); my $cgi = new CGI; my $upload_dir ='/home/gecko/www/htdocs'; my $des_dir ='/home/gecko/geckodev/cgi-bin/uploads'; my $file = $cgi->param('pi_file_name'); my $filename = $file; $filename =~ s/^.*(\\|\/)//g; open(FILELIST,"<$filename") || die print "Fail to upload: $!"; while(<FILELIST>) { $line=$_; chomp($line); my $doc = $line; $doc =~ s/^.*(\\|\/)//g; $line =~ s/^.*(\\|\/)//g; open(OUT,">$upload_dir/$doc") || die print "Fail to upload: $!"; + open(FILELIST1,"<$line") || die print "Fail to upload: $!"; while(<FILELIST1>) { print OUT $_; # print $_; print $cgi->header(); print "<HTML>"; print $_; print "<BODY bgcolor=#FFFFFF>"; print "<BODY bgcolor=#FFFFFF>"; print "</BODY>"; print "</HTML>"; } close(OUT); }

20050606 Janitored by Corion: Added formatting

Replies are listed 'Best First'.
Re: Upload Multiple Files
by thundergnat (Deacon) on Jun 06, 2005 at 14:58 UTC

    There are so many things wrong with this, it is difficult to know where to start.

    First, put <code> </code> blocks around your code so it will be easier for others to help you.

    When you are developing a script, always

    use warnings; use strict;

    They will save you a lot of aggravation in the long run. You can comment them out after the script is finished, but during development, let the computer give you all the help it can.

    Look into using the File::Basename module instead of regexes for getting the base file name.

    You probably will want to use the $cgi->upload() function to handle the uploads.

    If the FILELIST file is remote, (on the users hard drive as opposed to the servers,) you are going to have to upload that first before you can use it.

    There are several places where you have two variables, equal to each other, where you perform the same transform operations on each. Useless added complexity.

    In the output, you output a header with $cgi->header(), but then print out some more header elements, then the "text" of the page then open the BODY (twice!!) and close it again immediately.

    I think you should work on writing a script that will upload ONE file, get it working reliably, and then work on expanding it to handle multiple files. I would also highly recommend reading the docs for the CGI module. Several times.

Re: Upload Multiple Files
by derby (Abbot) on Jun 06, 2005 at 19:42 UTC
    You can't do that. When dealing with file uploads on the server side, the files *have all ready been uploaded* to a temporary location, you cannot reach *back* to the client from the server. What you could do is upload a file containing a list of files and then return a form that contains the files to be uploaded pre-filled - just awaiting the user to hit submit (or if you're really evil - add a javascript handler to autosubmit - hmmm can that be done?). You may also want to use CGI's upload function (or at least read the *CREATING A FILE UPLOAD FIELD* section of CGI).
    -derby
    Update: Doh! The AM below is correct. Most browsers will not pre-fill a file input field.
      For exactly the security reason you mentioned, input form fields for file uploads cannot be prepopulated.
        Hi, Thanks for your inputs. Could you please suggest any other way through which we can upload the contents of the directory on the server
Re: Upload Multiple Files
by reasonablekeith (Deacon) on Jun 06, 2005 at 14:33 UTC
    hi, arun_amiable, could you please edit your post and put <code> </code> tags round your code (and fix your title spelling mistake). You'll get a much better response.
    ---
    my name's not Keith, and I'm not reasonable.