Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

using uploadInfo from CGI.pm

by c (Hermit)
on Nov 30, 2001 at 05:52 UTC ( [id://128529]=perlquestion: print w/replies, xml ) Need Help??

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

In the CGI.pm documentation, it shows the following example:

$file = $query->upload('uploaded_file'); if (!$file && $query->cgi_error) { print $query->header(-status=>$query->cgi_error); exit 0; }

This lets you do a check on the MIME type of a file being uploaded, provided that the browser passes this type of info for the file. I've tried using this syntax with both Netscape 4.7 and IE 5.5, but neither seems to pass the information. With fatalstobrowser enabled, I receive:

Can't use an undefined value as a HASH reference at /path/to/search-ho +wto.cgi line 185.

My code verbatim, is:

sub upload_image { ## pull the file in a variable my $fh = $query->upload('upload'); ## determine the file type via the headers my $type = $query->uploadInfo($fh)->{'Content-Type'}; ## take action based on type if ($type eq 'text/html') { ## hash of file names to write to my %destination = ( 'match' => "match.html", 'no_match' => "no_mat +ch.html"); ## open the file to be written open(HTML, ">/www/$destination{$formdata{filename}}") or die "Cant open HTML file for writing : $!\n"; ## if file is determined to be text/html write contents to appropr +iate location while(<$fh>) { print HTML "$_\n"; } close(HTML); ## otherwise set error for later notification within html } else { $error = "on"; } }

When I remove the uploadInfo line, the file gets uploaded properly, however its not worth it for me to remove this line. Can someone point out what I am missing?

-c

Replies are listed 'Best First'.
Re: using uploadInfo from CGI.pm
by dws (Chancellor) on Nov 30, 2001 at 07:55 UTC
    Wild guess (based on comparing your code to my upload code). Instead of
    ## pull the file in a variable my $fh = $query->upload('upload'); ## determine the file type via the headers my $type = $query->uploadInfo($fh)->{'Content-Type'};
    try
    ## pull the filename in a variable my $fname = $query->param('upload'); ## determine the file type via the headers my $info = $query->uploadInfo($fname); my $type = $info->{'Content-Type'};
    I take an extra step, but found that having $info where I could get at it with the debugger paid off (once, which was enough).

      Thanks for your attention to this post. Its very appreciated. Unfortunately, it hasnt resolved itself. I've done some testing of my own extrating just the portion of the script that uploads a file and check the content-type printing out the value of $type. It consistantly comes up as undef. My code is now:

      sub upload_image { ## pull the file in a variable my $fh = $query->param('upload'); ## determine the file type via the headers my $info = $query->uploadInfo($fh); my $type = $info->{'Content-Type'}; ## take action based on type if ($type =~ m|text/html|i) { ## hash of file names to write to my %destination = ( 'match' => "match.html", 'no_match' => "no_mat +ch.html"); ## open the file to be written open(HTML, ">/path/to/$destination{$formdata{fi lename}}") or die "Cant open HTML file for writing : $!\n"; ## if file is determined to be text/html write contents to appropr +iate locat ion while(<$fh>) { print HTML "$_\n"; } close(HTML); ## otherwise set error for later notification within html } else { $error = "|$type|"; } }

      I've tried several different kinds of files, and still to no avail. When I print out the value of $error, i just end up with || making me believe that its being returned as undef. I'm going to keep hammering on it, but I am bewildered as to what is happening.

      thanks -c

        If $info->{'Content-Type'} is coming up undef, try
        my $info = uploadInfo($fname); print "uploadInfo:<br>"; map {print " $_: " . $info->{$_} . "<br>\n" } sort keys %$info;
        to see what you are getting.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found