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

After scaning SuperSearch and trying all the ways, I can't figure out what's wrong.

Template file

<HTML> <HEAD> <TITLE>Upload</TITLE> </HEAD> <BODY> <FORM ACTION="/perl-cgi/upld" METHODE="POST" ENCTYPE="multipart/form- +data"> <INPUT TYPE="FILE" NAME="datei"> <P> <INPUT TYPE="SUBMIT"> </BODY> </HTML>

cgi file

#!/usr/bin/perl use warnings; use strict; use HTML::Template; use CGI; my $template = HTML::Template->new(filename => 'upld.tmpl'); my $cgi = new CGI; if ($cgi->param('datei')) { loadpic($cgi); exit; } print "Content-Type: text/html\n\n"; print q(<link rel="stylesheet" type="text/css" href="/pds392/style.css +"> <style type="text/css"></style>); print $template->output; sub loadpic { my $cgi = shift; my $file = $cgi->param('datei'); my $fh = $cgi->upload('datei'); print "<HTML><BODY>$file<br></BODY></HTML>"; open OUT, ">/tmp/1.pic.jpg"; my $buffer; while (read $fh, $buffer, 512) { print OUT $buffer; } print "Content-Type: text/html\n\n"; close OUT; exit; }

my $fh is allways undef and the script dies, but $file contains the choosen filename!!!
Why ? Can this behavior result of using this script with mod_perl ? Or is there something I've missed ?

Cheers

-----------------------------------
--the good, the bad and the physi--
-----------------------------------

Replies are listed 'Best First'.
Re: CGI upload and mod_perl
by bassplayer (Monsignor) on Feb 19, 2003 at 23:56 UTC
    You have misspelled METHOD in your FORM tag. I believe that this would cause the form to default to GET, which is probably why your upload is failing.

    bassplayer

      Thanks !!!!!

      :-)
      -----------------------------------
      --the good, the bad and the physi--
      -----------------------------------
      
•Re: CGI upload and mod_perl
by merlyn (Sage) on Feb 19, 2003 at 23:59 UTC
      Probably I misunderstood the meaning of mod_perl ?
      I'll have a look in the documentation ;-)
      -----------------------------------
      --the good, the bad and the physi--
      -----------------------------------
      
        mod_perl is a Perl interpreter embedded in an Apache server process, and requires the use of handlers and directives to control those handlers. mod_cgi is CGI: a language agnostic handler that forks a separate process to run a program, passing some information via environment variables and other information via standard input, expecting the response to come via standard output and an exit code.

        Some CGI programs are written in Perl. But it's either mod_perl or mod_cgi handling it. Never both.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: CGI upload and mod_perl
by hv (Prior) on Feb 20, 2003 at 00:03 UTC

    I have very similar code working successfully (with perl 5.005_03 and CGI 2.752, but not mod_perl - it might be that).

    The relevant bits are:

    my $file = $cgi->param('datei'); my $fh = $cgi->upload('datei'); # and then I just do: local $/; $text = <$fh>;

    Hugo

      You might not want to suggest to others to use that bit of code. Slurping an entire uploaded file into a scalar is a Bad Thing(Anyone know of a trademark on this? {grin}). What if I want to upload a 10MB file? You're going to have to hold 10MB in memory. Bad. Instead, you should be using something like this, dealing with one line at a time:

      my $file = $cgi->param('datei'); my $fh = $cgi->upload('datei'); while (my $text = <$fh>) { # Do something... like this maybe: print $out_file $text; }

      You might even want to consider looking at read() to read in a specific number of bytes on each pass through the loop. I generally don't go as far as to do that though. So what if my binary uploads might slurp a little more if there's a large chunk of fle with no newlines in it? :) (P.S: I won't even pretend to know how frequently newlines appear in a binary file. All I know is that they are there.)


      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

        Sorry, perhaps that example wasn't good for general use. In my case the file's contents are going to be pumped straight into a database blob, and I don't see a useful alternative to grabbing the whole file - trying to sysread a chunk at a time to do repeated appends on the database field seems like overkill. I'm somewhat protected also because I've got an authenticated user before it gets this far.

        Hugo

        Good to know {evil-grin}, if I know one of your scripts is running I can feed it into oblivion with non-newline bytes {mad_laughter}.
        As a rule of thumb: You never know!

        regards,
        tomte