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


I'm trying to use CGI's upload hook feature to keep track of the progress of large file uploads. According to the docs,

The $use_tempfile field is a flag that lets you turn on and off's use of a temporary disk-based file during file upload. If you set this to a FALSE value (default true) then param('uploaded_file') will no longer work, and the only way to get at the uploaded data is via the hook you provide.

I want to turn off temp files and handle the data myself. Unfortunately, when I set this value to 0, my hook does not seem to ever get called.

Here's some code which works:

# override query object creation so we can install # our hook at instantiation sub cgiapp_get_query { my $self = shift; my $q = CGI->new( sub { $self->_ul_hook( @_ ) } ); return $q; } sub _ul_hook { my $self = shift; my ( $filename, $buffer, $bytes ) = @_; print STDERR "$filename: $bytes\n"; }

This code works fine and my hook gets called, but it creates a temporary file which I don't want.

When I change the constructor to this,

my $q = CGI->new( sub { $self->_ul_hook( @_ ) }, undef, 0 );

...the hook does not get called. (At least, there's no evidence of it in the error log.)

Thanks for any help.

Update: It also appears to fail if I explicitly set the third parameter to 1. Apparently it only works with the third parameter unspecified, but I don't want the default behavior. :(

Replies are listed 'Best First'.
Re:'s upload hook with tempfile off
by robartes (Priest) on Nov 10, 2006 at 08:51 UTC
    Which version of CGI are you using (try perl -MCGI -e "print $CGI::VERSION"), and what is the error you get when you set the use_tempfile parameter to 1?


      I'm using CGI version 3.15. I don't get any errors in the logs when I set the parameter to 1 or 0, it just fails to call the hook.

        It seems that CGI 3.15, which you have, does not have the use_tempfile parameter. The parameter you provide in your call to CGI::new() gets passed on to the 'normal' CGI initialisation (i.e. the code that builds the query contents), probably leading to all kinds of funny stuff behind the scenes.

        Try upgrading to CGI version 3.25, which does support the use_tempfile parameter. I have not checked whether it actually does anything yet, but at least you can set it in 3.25, as opposed to 3.15.

        Hope this helps you.