Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Uploading Multiple Files

by cal (Beadle)
on Sep 27, 2002 at 20:22 UTC ( [id://201324]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,
Since there is no way to hardcode a file path as a "value" in the FILE UPLOAD form as stated in RFC1867. I would like to find an alternative to allow multiple uploads. The files always have the same names and paths. Because I am still new to perl my scripting creativity is very limited.

I had an idea that I want to see if it is possible.

Since the name of the FILE fields are named FILE1 FILE2 FILE3 FILE4 FILE5 etc. I thought of having a hash that would contain the file paths of the files I wanted to upload(shown below).Then if the passed value of param("FILE$a")was empty it could look up the stored file path in the hash.

After looking at this it may be I don't require a FILE FIELD INPUT at all and may be just need to hard code all of the file paths anyway.
Any ideas
Thanks
Cal
######################################### for(my $a = 1; $a <= 5; $a++) { my $req = new CGI; if($req->param("FILE$a")) { my $file = $req->param("FILE$a"); my $filename = $file; } } ########################################## my %path_hash = ( FILE1 => C:\My Documents\Online_Bulletin\pg2.html, FILE2 => C:\My Documents\Online_Bulletin\pg3.html, FILE3 => C:\My Documents\Online_Bulletin\_bk.gif, FILE4 => C:\My Documents\Online_Bulletin\img0.gif, FILE5 => C:\My Documents\Online_Bulletin\img1.gif, );

Replies are listed 'Best First'.
Re: Uploading Multiple Files
by swiftone (Curate) on Sep 27, 2002 at 20:30 UTC
    I don't totally understand what you're trying to do, but here are a few comments that I hope can help you:
    • Why are you creating multiple CGI objects? I think you want to create a single object, and read the values from that.
    • CGI has an upload() function for uploading files, and the docs offer a recommended idiom.
    • You don't need to name your fields different things -- The upload() function can be used in array context (see docs)
    • Not all browsers send the entire path of the upload (Netscape, at least my netscape, sends only the filename, no path) So whatever you are trying to accomplish, they'd break your hash idea..

    Update: I think I've figured out what you are trying to do: You want to force the page to upload the given files? Can't be done on the perl side. Perhaps javascript, but the actual sending of the file is decided on the browser side, and perl can only work with what it's given. Of course, if I'm wrong about your goal, please clarify.

Re: Uploading Multiple Files
by sauoq (Abbot) on Sep 27, 2002 at 20:35 UTC

    I'm very confused about what you are trying to do here. You do realize that the file upload mechanism is a way for the client (browser) to send a file to the server, right? It looks to me like you are attempting to use it to send a file to the client.

    Also, there would be no good reason to have the line my $req = new CGI; inside a loop like that.

    Can you explain exactly what you are hoping to accomplish?

    -sauoq
    "My two cents aren't worth a dime.";
    
      I want to send a pre-defined list of files to a server.
        I'm afraid might be in a dead end here. I don't think the server can request just any file from the client, that would be like a MAJOR security breach (f.ex. every server would have a file upload, which would discard the name and upload /etc/passwd or the Windows registry or something).

        I think the uploaded file's name is in the request that the client sends, and if I remember correctly, the uploaded file comes next. I don't think the webserver is even allowed to say anything before the entire file is uploaded, and therefore you can't request a file that way, but I'm on very thin ice here. Anyway, it sounds very dangerous to me.

        The server can't tell the client what files to send. (Hopefully not, at least.) As fsn points out, that would be a huge gaping security hole.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Uploading Multiple Files
by zentara (Archbishop) on Sep 28, 2002 at 17:22 UTC
    Hi, just in case you would be willing to run a script from the client, here is a multi-file upload script.
    #!/usr/bin/perl use warnings; use strict; use HTTP::Request::Common qw(POST); use LWP::UserAgent; my $url ='http://zentara.zentara.net/~zentara/cgi-bin/uploadz'; my $dir = shift || "."; opendir DIR, $dir or die "can't ls $dir: $!"; my @files = grep { $_ ne "." and $_ ne ".." } readdir DIR; foreach my $file (@files){ my $ua = new LWP::UserAgent; my $req = POST $url, Content_Type => 'multipart/form-data', Content => [ file => [$file] ]; my $res = $ua->request($req); if ($res->is_success){print $res->as_string; }else{print $res->status_line; } } exit 0;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://201324]
Approved by fglock
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found