http://www.perlmonks.org?node_id=202142

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

Hi Everybody

I tried to incorporate file uploading into an exsiting Perl script and I failed. So I thought I would search around on Perlmonks, I did, and I found lots of useful info on how to create a file upload script however my exsisting script is messy so I thought i would try something nice and simple to start with

I want to select a file from a browse box on a form, click submit, and have the contents of the file echoed back to the browser for display

The webpage is a simple multi content file submission form with a submit button (and I'm sure this end is right as the script prints the name of the file entered)

#!/apps/PERL5/bin/perl use CGI qw(:standard); use CGI::Carp qw/ fatalsToBrowser /; $cgi = new CGI; print header; my $b_File = $cgi->param('user_File01'); print p("b_File ($b_File)"); my $fh = $cgi->upload($b_File); while (<$fh>){ print p("Data ($_)"); }

This must be a simple thing to do, and surely the above code should suffice?

It prints the name of the file to the screen (just the name no path info - Netscape7.0b) But does not print the details back to the browser for display?

I'm guessing it's something stupid that I'm doing as I though I understood the basic CGI.pm concepts from the Lincoln Stein book and the O'Reilly CGI book.

Please help!!

Have a great day where ever you are

Mark

:o)

Replies are listed 'Best First'.
Re: cgi File Upload, No data pased through?
by PodMaster (Abbot) on Oct 01, 2002 at 23:37 UTC
    CGI Help Guide
    Web Programming with Perl

    Don't mix the OO and function oriented interface of CGI for no good reason.

    This is an example I use often to test cgi upload scripts.

    #!/usr/bin/perl -w #!C:/perl/bin/perl -w use CGI::Carp qw( fatalsToBrowser ); use CGI; #use CGI 2.7; # the minimum acceptable version is 2.7 in my book use strict; my $query = new CGI; print $query->header, $query->h1("VERSIOn $CGI::VERSION"), $query->h1("POST MAX $CGI::POST_MAX "), $query->h1(" DISABLE_UPLOADS $CGI::DISABLE_UPLOADS "), $query->start_multipart_form(); print $query->filefield(-name=>'uploaded_file', -default=>'starting value', -size=>50, -maxlength=>80); print $query->submit(),$query->end_form(); print $query->hr(); if($query->upload('uploaded_file')) { my $fh = $query->upload('uploaded_file'); print "<PRE>Filename: $fh \n"; print "Size: ".(-s $fh)."\n\n"; print while <$fh>; # idiomatic print "</pre>"; }
    If this does not work or reveal why not (old/broken version of CGI.pm, disabled uploads ...), then you need to check your web server configuration (disabled/limited at server level).

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: cgi File Upload, No data pased through?
by blm (Hermit) on Oct 02, 2002 at 00:19 UTC

    I was getting a similar problem with my upload.cgi. No data that the file contained appeared to be uploaded. I fixed it with help from virtualsue and boo_radley (I think? I'm sorry I can't remember :-( ) however I am not sure what exactly was the fix. I even tried a binary search through the patches I applied with no luck. Make sure that $cgi->upload is not returning undef and check whether you are not Suffering from Buffering. Also check your log files, use warnings and use strict

    --blm--
Re: cgi File Upload, No data pased through?
by Moonie (Friar) on Oct 01, 2002 at 23:27 UTC
    First off, here's an article that will help you some - here. Also, you will probably find many different answers - try Super Search as well. Good luck!

    - Moon
Re: cgi File Upload, No data pased through?
by Joost (Canon) on Oct 02, 2002 at 13:48 UTC
    Your problem might just be that you forgot to set method="POST" and/or enctype="multipart/form-data" in your form tag.

    Also, as mentioned above, never mix OO and procedural calls to CGI.pm

    -- Joost downtime n. The period during which a system is error-free and immune from user input.