Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^8: file upload and IO::Handle

by rowdog (Curate)
on Sep 09, 2010 at 19:42 UTC ( #859550=note: print w/replies, xml ) Need Help??

in reply to Re^7: file upload and IO::Handle
in thread file upload and IO::Handle

I still cannot figure out hook, the values passed to it(or jus, received (why not passed, or where passed from?)) and what executes it.

Well, CGI explains all that in Progress bars for file uploads and avoiding_temp files but what happens is, CGI has a socket for the upload, every time it reads a chunk from the socket, it calls the hook with the new data, which is where your parameters are coming from.

Replies are listed 'Best First'.
Re^9: file upload and IO::Handle
by Anonymous Monk on Sep 09, 2010 at 20:56 UTC
    Ok, I followed the CGI recommendation for setting it:
    $q = CGI->new(\&hook [,$sess_ref [,$use_tempfile]]);
    and I get this error:
    syntax error at /home/path/to/Files/ line 5, near +"&hook ["
    which is the line I showed you above...
    why does that not work? $sess_ref is the session reference to variable to use the session stuff, the CGI documetion says $data is optional and can be the database handle, so I figured I may as well pass the session reference so it can use the session they are in, to update the status. but the error happens.

    Also, inside of the hook, I have it open a temp file and write data to it, always appending it so that I can confirm it is reaching inside the hook, but nothing ever gets written and no die error is executed so I know it is just not getting executed. How could that be?

    Thanks again,

      Get rid of the [

      $q = CGI->new(\&hook [,$sess_ref [,$use_tempfile]]);

      That's the example from CGI showing the optional syntax. What it means is that you can use it like

      $q CGI->new(\&hook);
      $q = CGI->new(\&hook, $sess_ref);
      $q = CGI->new(\&hook, $sess_ref, $use_tempfile);

      Once you get that squared away, you should see hook getting called.

        Thanks rowdog, that got rid of the error, still not working however... tried it with the session they are logged in with and with a temp session both:
        ### Filename: ## subs in file: #hook $| = 1; $q = new CGI (\&hook, $sess_ref); # CGI->new(\&hook, $sess_ref); ## T +ried it with both of these, neither worked # Also tried both of these: #$q = new CGI (\&hook, $sess_ref, $use_tempfile); # CGI->new(\&hook, $ +sess_ref, $use_tempfile); # Neither worked $_sloc = '/tmp/s'; # This is the file upload hook, where we can update our session # file with the dirty details of how the upload is going. sub hook { my ($filename,$buffer,$bytes_read,$file) = @_; my ($sessid) = $ENV{QUERY_STRING}; my $length = $ENV{'CONTENT_LENGTH'}; my $percent = 0; if ($length > 0) { # Don't divide by zero. $percent = sprintf("%.1f", (( $bytes_read / $length ) * 100) ); } if($file) { $file->attr("_uploadUpdateStatus_",join(':',$bytes_read,$lengt +h,$percent)); } # Write this data to the session file. open (SES, ">$_sloc/$sessid.session"); print SES "$bytes_read:$length:$percent"; close (SES); } my $action = $q->param("view"); if ($action eq "upl") { # Make a file upload hook. # They are first submitting the file. This code doesn't really + run much # until AFTER the file is completely uploaded. my $sessid = $q->param("sessid") || $ENV{QUERY_STRING}; open (SES,">$_sloc/$sessid.session") or die "Could not locate +file: $! $_sloc/$sessid.session;"; print SES "1:1:1"; close (SES); my $filename = $q->param("incoming"); my $handle = $q->upload("incoming"); $filename =~ s/(?:\\|\/)([^\\\/]+)$/$1/g; # Copy the file to its final location. open (FILE, ">/home$_website_username/www/Videos/$filename") o +r die "Can't create file: $!"; my $buffer; while (read($handle,$buffer,2048)) { print FILE $buffer; } close (FILE); # Delete the session file. unlink("$_sloc/$sessid.session"); # Done. $_tfile = "/home$_website_username/www/Videos/" . $filename; $type = uploadInfo($filename)->{'Content-Type'}; ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$c +time,$blksize,$blocks) = stat($_tfile); if(-e "/home$_website_username/www/Videos/$filename") { $sess_ref->attr("show_message","success|I have successfull +y uploaded $filename to:$filename<br /> +File type: $type<br />File size: $size bytes<br />$_page_content<br / +>"); window_redirect("$_url?page=$in{page}&do=$in{do}$sess_id") +; } else { $sess_ref->attr("show_message","error|I could not upload t +he file: $filename to:$filename<br />Fi +le type: $type<br />File size: $size bytes<br />$_page_content"); window_redirect("$_url?page=$in{page}&do=$in{do}$sess_id") +; } } elsif ($action eq "ping") { # Now the meat of the CGI script. print "Content-Type: text/html\n\n"; # Checking up on the status of the upload. my $sessid = $q->param("sessid"); # Exists? if (open(OUTF, "$_sloc/$sessid.session")) { my $data = <OUTF>; close (OUTF); print "$data"; } elsif($sess_ref) { $data = $sess_ref->attr("_uploadUpdateStatus_"); print "$data"; } else { print "Error reading Upload progress...$sessid $! $_sloc/$ +sessid.session"; } if($sess_ref) { $sess_ref->close(); } if($dbh) { $dbh->disconnect(); } exit; } else { my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 ); $_newSessId = $_un . join("", @chars[ map { rand @chars } ( 1 +.. 10 ) ]); $_page_content .= qq~ <div id="webform"> <fieldset id="upload_form"> <legend>Approved Upload System</legend><br /> <br /> Hello, you have been verified as allowed to upload + files to the Videos directory for the .com website...<br /> <br /> Please use the system below to upload files to thi +s directory.<br /> <br /> ~ . start_multipart_form(-name=>"uploadFile", -act +ion=>url() . '?' . $_newSessId, -method=>"POST", -onSubmit=>"return s +tartUpload();", id=>"theform") . hidden(-name=>"page") . hidden(-name=>"do") . hidden(-name=>"sessid", -value=>$_newSessId, -over +ride=>1) . hidden(-name=>"view", -value=>"upl", -override=>1) + . $hidden_sess_id . filefield(-name=>'incoming', -class=>'formfield', -size=>50, -maxlength=>80) . qq~<br /> <br /> ~ . submit(-name=>"choice", -value=>"Upload The File", -cl +ass=>"submit") . qq~<br /> <br />~ . end_form() . qq~ </fieldset> <div id="progress-div" style="display: none; width +: 600px; margin: auto"> <fieldset style="width: 600px;"> <legend>Upload Progress</legend><br /> <br /> <div id="trough"> <div id="bar" style="width: 0%"></div> </div> Received <span id="received">0</span>/<span id +="total">0</span> (<span id="percent">0</span>%)<br /> <div id="debug">Uploading</div> </fieldset><br /> <br /> </div> <br /> <br /> </div><br /> <br /> ~ . q~ <script type="text/javascript"> // a jquery-like function, a shortcut to document.getElementBy +Id function $(o) { return document.getElementById(o); } // This function is called when submitting the form. function startUpload() { // Hide the form. document.getElementById('upload_form').style.display = "no +ne"; // Show the progress div. document.getElementById('progress-div').style.display = "b +lock"; // Begin making ajax requests. setTimeout("ping()", 1000); // Allow the form to continue submitting. return true; } // Make an ajax request to check up on the status of the uploa +d function ping() { var ajax = new XMLHttpRequest();"GET", "index.cgi?page=~.$in{page}.q~&do=~.$in{d +o}.q~&view=ping&sessid=~.$_newSessId.q~&" + "rand=" + Math.floor(Math +.random()*99999) + "~.$sess_id.q~", true); ajax.onreadystatechange = function () { if (ajax.readyStat +e == 4) { parse(ajax.responseText);}}; ajax.send(null); } // React to the returned value of our ping test function parse(txt) { var parts = txt.split(":"); document.getElementById("debug").innerHTML = document.getE +lementById("debug").innerHTML + "." + parts[0] + ';' + parts[1] + ';' + + parts[2] + ';'; if (parts.length == 3) { document.getElementById("received").innerHTML = parts[ +0]; document.getElementById("total").innerHTML = parts[1]; document.getElementById("percent").innerHTML = parts[2 +]; document.getElementById("bar").style.width = parts[2] ++ "%"; } // Ping again! setTimeout("ping()", 1000); } </script>~; } 1;
        What am I doing wrong? I cannot seem to get it working, even if I have the hook open a debug file and write anything to it, nothing happens. I tried it just using $sessid.session and when that did not work, I created a global file in: /tmp named: s with 777 permissions, still it does not work (we are on a dedicated server behind a firewall so I did not mind doing that, I know it is safe).

        Any idea why this does not work?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://859550]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-03-25 15:42 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (311 votes). Check out past polls.