Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found