Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

CGI.pm: upload() succeeds, uploadInfo() fails

by Llew_Llaw_Gyffes (Scribe)
on Jul 21, 2005 at 20:01 UTC ( [id://476979]=perlquestion: print w/replies, xml ) Need Help??

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

I have a web gallery application which, not unreasonably, allows photo uploading. The code that handles uploads, stripped of redundant parts, is essentially this:

$q = CGI->new(); %form = $q->Vars; ... $file = $form{photo}; $fh = upload('photo') || error ($q,"Cannot upload file $file"); $info = uploadInfo($q->param('photo')) || error ($q,"Could not get + information about $file!"); unless ($errors) { $type = $info->{'Content-Type'}; unless ($type =~ m,^image/(jpeg|gif|png)$,) { error ($q, "File type '$type' is not permitted"); } ...

When I test this code from the submission form, all the form fields appear correct, upload() succeeds, but uploadInfo() fails. In the absence of the check for errors, this causes $info->{'Content-Type'} to throw an 'undefined value as HASH reference' error. $q->cgi_error sheds no light on the failure.

Can anyone suggest why uploadInfo() is failing, even though upload() has succeeded?

Replies are listed 'Best First'.
Re: CGI.pm: upload() succeeds, uploadInfo() fails
by geektron (Curate) on Jul 21, 2005 at 20:15 UTC
    well:
    1. Which webserver?
    2. Are you sure the file has uploaded? If you don't have the right enc-type on the form, the file doesn't actually get up, even though i've had cases where $q->upload() doesn't throw an error. (hint: check the location of your saved file ... )

      Sorry for the omissions. The webserver is Apache 1.3.x, and I've verified that the file is actually getting uploaded.

      My original version of the CGI was straight Perl without using CGI.pm (I prefer not to use CGI.pm, as I feel it unnecessarily obfuscates many simple things), and I was very obviously receiving the file data but just couldn't figure out how to extract it from the multipart/form-data stream without using CGI.pm. I didn't include in my code snippet the file saving code I wrote for the CGI.pm version, because I didn't think it was relevant to the problem, but it does in fact save the file (correctly, and uncorrupted) ... I just can't get any information about the file, either before or after saving it.

      Incidentally, with regard to enc-type, HTMLhelp.com's HTML4 reference page on the input element says, in part:

      The ACCEPT attribute gives a comma-separated list of media types accepted, allowing the browser to filter out inappropriate files. Current browsers generally ignore the ACCEPT attribute.

      I'm using ACCEPT="image/jpeg/image/png,image/gif" anyway, but I'm not relying on it. This is why I want to use uploadInfo() instead of just assuming the user uploaded the correct file type.

Re: CGI.pm: upload() succeeds, uploadInfo() fails
by kutsu (Priest) on Jul 21, 2005 at 20:22 UTC

    At a guess, you should be doing $q->upload() not upload() (same with uploadInfo).

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      That was one of my speculations, too, and I've already tried it (I've gone through probably a dozen revisions of this code trying to either make it work or figure out why it isn't), but doing so didn't make any difference. The q-> is currently back in again, but has not affected the behavior.

        ...but doing so didn't make any difference
        It makes a difference since you're not supposed to mix the two use styles.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found