Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: CGI.pm file upload freaking me out

by chromatic (Archbishop)
on Jan 06, 2001 at 23:35 UTC ( #50274=note: print w/ replies, xml ) Need Help??


in reply to CGI.pm file upload freaking me out

I've also had trouble with this. There's a bit of disconnect between the docs I've read and the behavior I've actually managed to get working. Here's my test code that simply displays the image to the browser:

#!/usr/bin/perl -wT use strict; use CGI qw (:standard); use CGI::Carp qw( fatalsToBrowser ); my $filename; if (defined($filename = param('filename'))) { my $upload = upload('file'); print header(-type=>uploadInfo($upload)->{'Content-Type'}); print while <$upload>; } else { print header(); print <<HTML; <html><body bgcolor="#ffffff"> <form method="POST" enctype="multipart/form-data"> <input type="text" name="filename"><p> <input type="file" name="file"><p> <input type="submit"> </form> HTML }
The big difference in this code is that I used the name of the upload parameter instead of the filename when calling upload(). Contrary to what the Mouse book says, this works for me.

For what it's worth, I'm using CGI.pm 2.68, so this may have changed slightly in a newer version. The only mention of uploads in the ChangeLog is of a one-character bug squashed in 2.74. Your mileage may vary.


Comment on Re: CGI.pm file upload freaking me out
Download Code
Tweaking the upload script for Ascii
by dws (Chancellor) on Jan 07, 2001 at 01:30 UTC
    My upload script does pretty much the same thing, with one tweak to handle newline conversion when uploading text from a Wintel box. (My ISP has CGI.pm 1.45, and I haven't had noticed any problems uploading.)
    my $stripCRs = param('filetype') eq 'text'; ... while ( <$upload> ) { s/\r// if $stripCRs; print UPLOAD $_; }
    Getting a pair of radio buttons on the upload form for 'text' and 'binary' is left as an exercise.
Re: Re: CGI.pm file upload freaking me out
by Trimbach (Curate) on Jan 07, 2001 at 02:21 UTC
    I tried your test script and it works perfectly on my server, but after changing my code a bit to reflect yours it still wasn't working. So... the problem wasn't with CGI, but rather with something I did somewhere else in my script.

    After some investigation I discovered that apparantly at some point in my development (remember, this is old code) I switched from the OO-style CGI interface to the functional interface, with about 99% using param(blah) and the rest using $object->param(blah). CGI.pm apparantly doesn't have a problem when you mix interfaces for most functions (my HTML-generation and parameter fetching all worked fine) but it apparantly hoses the magic upload functions. Imagine that. :-D Once I took out $object = new CGI; everything miraculously started working.

    Remember what I said at the beginning? Well, I'm an idiot. Although I didn't intend to, the lesson here is obvious: don't mix your interfaces in CGI.pm.

    Thanks to all who took the time to post... I sure do appreciate it!

    Gary Blackburn
    Trained Killer

      Hmm. I wonder if this was at the core of the uploadInfo issue. I just got stuck in the same spot with the hash ref error and tried using 100% OO and 100% functional, and I got the same error both ways. Is it possible some files just wouldn't have a mime type associated with them?

      update: I have to take that back: I found a "$q = new CGI" in an included module which I think was related. When I used this existing query object to call uploadInfo, it started working.

      -mark

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://50274]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2014-10-01 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (390 votes), past polls