I'm trying to do some funky things with HTTP file uploading, i'm having trouble getting
CGI to work for me. Basically, it seems that i've stumbled upon a catch 22 that ends in: you can't have access to anything in your CGI variable until you're done reading all of <STDIN> (which may be substantial, and thus my very desire to acess that OTHER info first). The problem can be explained like so, by creating a CGI object STDIN is parsed and the contents of any uploaded files are read into a temp file, using the "upload_hook" feature available, you cannot yet have access to the contents of a CGI variable because it isnt' created yet, and creating one will try to parse STDIN and run your upload hook, and on and on...
let's get contextual, a simple example showing that I can't access $cgi, and therefore (right?) any of the other CGI variables that got POSTed with the file! here's a true blue script you can toss on a server and hit, it'll show you a form and when you submit it if you commented out the "ERROR!" line, it'll print the contents of your uploaded file (we suggest you upload a small text file)
#!/usr/bin/perl
$|++;
use strict;
use warnings;
use CGI;
unless( $ENV{REQUEST_METHOD} eq 'POST' ) {
#show the itty bitty upload form:
my $cgi = new CGI;
print $cgi->header() . qq{
<form action="file_upload" method="post" enctype="multipart/form
+-data">
<input type="file" name="myFile">
<input type=submit>
</form>
};
}
else {
print "Content-Type: text/plain\n\n"; # [sic] can't use CGI yet, 'me
+mber?
sub hook;
my $cgi = new CGI( \&hook, undef, 0 );
#use the hook, and don't use a tempfile, i'll handle that myself,
#don't pass any "data" to hook either, what would i pass?
my %uploads;
sub hook {
my ($filename, $buffer, $bytes_read, $data) = @_;
my $fh;
unless( exists $uploads{$filename} ) {
#oh yeah, and $filename might not be unique either, but what other cho
+ice do i have?! this condition tells me that it's our first time on t
+his file
my $somevar = $cgi->param('somevar'); #ERROR!
#Can't call method "param" on an undefined value
warn "Getting an upload for $filename";
$uploads{$filename} = 1;
}
print $buffer if $buffer; #back at ya!
}
}
now you may say "well, you don't even need the $cgi variable in your hook!" and i'll tell you that i do, this is only a demo. ;) specifically, if i could have access to the form name that contained the file the hook is currently running against, i'd be happy. (in this case that would be "myFile")
thanks ye brothers and sisters, your thoughts i will treasure.
It's not what you look like, when you're doin' what you’re doin'.
It's what you’re doin' when you’re doin' what you look like you’re doin'!
- Charles Wright & the Watts 103rd Street Rhythm Band
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.