It seems that CGI.pm has a nice little bug that gets in the way of normal filehandle processing. I was trying to write a simple perl/cgi file upload script to upload binary files as blobs into MySQL and ran into this nifty little issue.
The problem seems to be that cgi.pm messes with the filehandle to make it more "lightweight/nonusable". I built a simple html form using an input type of of file and a enctype of multipart/form-data. Using the input "file" type browse button to select a file off the local filesystem and submitting the form renders this output in my browser:
Filename => dos_performance.xls
Size => 106496
Content-Type: text/html; charset=ISO-8859-1 Undefined subroutine Fh::s
+eek at /usr/lib/perl5/site_perl/5.8.6/File/MimeInfo/Magic.pm line 97
Here is the subroutine I have the form send to:
sub upload {
my ($obj, $db) = @_;
my $project_id = $PTS::QUERY_STRING{'pid'};
my $user_id = $PTS::QUERY_STRING{'uid'};
my $fname = $PTS::QUERY_STRING{'fname'};
my $filehandle = $PTS::CGI->upload('fname');
my $size = -s $filehandle;
print $PTS::CGI->header();
print "Filename => $fname\n<br>";
print "Size => $size\n<br>";
push @Fh::ISA, 'IO::Seekable' unless $filehandle->isa('IO::Seekabl
+e');
push @Fh::ISA, 'IO::Handle' unless $filehandle->isa('IO::Handle');
print "Mime Type => ".File::MimeInfo::Magic->magic($filehandle)."\
+n<br>";
#show($obj, $db);
}
It seems that File::MimeInfo::Magic has a problem with the filehandle that is returned by CGI.pm. As you can see I have tried to re-add some of the missing parent objects but this still fails. AM I not doing this correctly? How would one go about checking the mimetype of data in buffer without having to write out a tmp file just to get the mimetype. I am not storing the file on the filesystem, but in a database so the intermediate step if writing the file out would waste cycles being written and then waste cycles having to be deleted.
Anyone have any ideas on how to get this working?