Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Uploading files using cgi.pm

by c (Hermit)
on Nov 29, 2001 at 19:25 UTC ( #128371=perlquestion: print w/replies, xml ) Need Help??

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

I've read over a few posts concerning uploading files and I am looking at pp152-5 in Lincoln Stein's book on using cgi.pm. Right now, I am just toying around with uploading a file from the local hard drive to the server. All I am doing in my current script is is uploading the file and then trying to print its contents to the web browser window. However, instead of the file's contents, I am just getting the file's name.

## cgi.pm parameters use CGI; #use CGI::Carp qw(fatalsToBrowser carpout); $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = 24000; my $query = new CGI; $query = CGI->new(); my %formdata; my @formfields = $query->param; for my $field(@formfields) { $formdata{$field} = $query->param($field); } print <<FORM; <center> <form method="post" action="https://www.$domain/$self" enctype="multip +art/form-data"> <input type="hidden" name="mod" value="on"> <input type="file" name="upload" size="40"><p> <input type="image" src="$images/submitbutton.gif"> </form> </center> FORM ## end upload documents ## while(<$formdata{upload}>) { print; }

This is just a snippet of my code. It seems to upload the file, however when a user selects C:\somefile.txt, the while statement just prints out this same path, minus the backslash. Based on Mr. Stein's code on p153 of his book with an example of how to print a file out to a local file on the server, I had thought the above snippet would work.

Can someone please point me straight?

humbly -c

Replies are listed 'Best First'.
Re: Uploading files using cgi.pm
by AidanLee (Chaplain) on Nov 29, 2001 at 19:39 UTC

    You might investigate the upload() function. From the CGI docs:

    $fh = $query->upload('uploaded_file'); while (<$fh>) { print; }
      I read through CGI.pm and see what you are referring to. The doc says that $fh will return a usable filehandle or undef if the param is not a valid filehandle.

      I changed my existing code to reflect your suggestion, however I kept my loop in place that stick forms values into a hash. So, I ended up with something like:

      my $fh = $query->upload($formdata{upload});

      This ended up coming back as undef, however once I removed my hash and made a direct query:

      my $fh = $query->upload('upload');

      it worked. Thanks for your help.

      -c

Re: Uploading files using cgi.pm
by cfreak (Chaplain) on Nov 29, 2001 at 22:17 UTC
    You don't need a filehandle, rather you need to use the read statement. So rather than what you have here:
    while(<$formdata{upload}>) { print; }

    Change that to:
    my $buffer; while(read $formdata{upload},$buffer,1024) { print $buffer; }

    you can also limit the size of the image being uploaded:
    my $buffer; my $max_size = 10 * 1024; # 10 KB but it can be whatever my $size; while(read $formdata{upload},$buffer,1024) { $size += 1024; if($size > $max_size) { print "Image is too big!"; last; } }
Re: Uploading files using cgi.pm
by cLive ;-) (Prior) on Nov 30, 2001 at 01:04 UTC
    Hmmm,

    When I get stuck I like to look through the Q & A section of the monastry. In this case I'd probably look through the CGI Programming section and look for the word 'upload'. This word then point me to How can I get started with a file upload script?.

    But that's just me, I suppose.

    cLive ;-)

    note - I have a stinking cold and sarcasm is as good as a hot toddy when I feel this groggy... ;-)

      I did take a look at that :) but i'm only interested in uploading text files, and I don't think I'll need to touch binmode. Maybe that should be renamed "How should I get started with an image upload script?".

      humbly -c

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2023-05-30 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?