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.


In reply to Re: simple CGI::upload_hook() guide or example? by cees
in thread simple CGI::upload_hook() guide or example? by thoughts

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.