http://qs321.pair.com?node_id=499616


in reply to simple CGI::upload_hook() guide or example?

OK, here is a completely stripped down version that just writes some data to a log file. This works fine on my machine

#!/usr/bin/perl use strict; use warnings; use File::Slurp; use CGI qw(:standard); my $query = CGI->new(\&_hook, 'ID'); # Grab the uploaded file my $file = $query->param('uploaded_file'); print header, start_html(-title => 'A Simple Upload Meter Example'), h +1('Upload Complete'), end_html; # This is the upload_hook that gets called repeatedly by CGI.pm # during an upload. It gets called once for every 4K of data read in # by CGI.pm sub _hook { my ($filename, $buffer, $bytes_read, $umid) = @_; write_file( '/tmp/filename', {append => 1 }, "Uploading $filename +($umid) - $bytes_read\n" ) ; sleep 1; }

And here is a simple HTML page that you can use to call it:

<html> <body> <form method="post" action="example.cgi" enctype="multipart/fo +rm-data"> upload file: <input type="file" name="uploaded_file" /><br />< +input type="submit" tabindex="2" name=".submit" /> </body> </html>

My gut instinct tells me that your test DID work, but your logfile was just buffering the output. To guarantee that it doesn't happen with this example, I am doing something very inefficient by opening and closing the log file on every call to the hook.

Also, I should note that the CGI.pm docs are not very clear in how to use the upload hook. CGI.pm needs to know about the hook when it is first used, so you have to call CGI::upload_hook(\&hook,$data); before you do anything else. And if you use the OO interface like I do, you must pass the hook in at creation time.