detect bytes written on abort



Here is an advanced PHP question. Can anyone think of a way to detect the number of bytes written to output when a script is aborted?

I am sending a large file to the client, and I want to record how many bytes are actually sent. I can detect abort of the script using a shutdown handler. In the shutdown handler, I tried ob_get_length, but it returns false. I tried to read the server's log file, but it is does not contain the information until the script fully quits. I tried to use fwrite to php://output, and then get the bytes written return value. However, if the script aborts in the middle of the write, then bytes written is never returned.

The only thing that worked was writing one byte at a time using fread/fwrite. But this also made the processor load 100% and download speed very slow (700KB/sec versus 24MB/sec on localhost using wget).

I know I can poll the web server's log file using a background process. But if I can do everything from within the script, the system becomes simpler and more responsive.

Below is the PHP 5.1.1 test script. I suspect it can not be done, but any advice would be appreciated!

sendfile.php

ignore_user_abort(false);
set_time_limit(60);
register_shutdown_function("handleShutdown");

$fp = false;
$fp = fopen("largefile.html", "rb");
fpassthru($fp);  // script is aborted in mid-execution

function handleShutdown() {
   global $fp;
   if($fp !== false) {
      fclose($fp);
   }
   $byteswritten = 0; // how to detect here?
   $shutdownmessage =
      "bytes: ".$byteswritten."\n".
      "status: ".connection_status()."\n".
      "aborted: ".connection_aborted()."\n";

   file_put_contents("shutdownlog.txt", $shutdownmessage);
}

Here is a windows batch file to run the request:

ECHO Hit Ctrl-C to simulate abort
IF EXIST sendfile.php.1 del sendfile.php.1
wget http://localhost/sendfile.php --tries=1
.



Relevant Pages

  • Re: unix shell script ignores exit when in function that is piped to tee
    ... > I thought calling a function within a script did NOT create sub-shells. ... is going to be able to alter the log file to cover up any inappropriate ... entry in the log to the previous entry. ... The easiest way to use crypt to ...
    (comp.unix.shell)
  • The necessity of Setting Objects to Nothing
    ... But what happens when you've got a script that is running as ... Dim Servers, List, strComputer ... Set Folder = objFSO.GetFolder ... '~~~ Write the results of the number of users query to a log file. ...
    (microsoft.public.scripting.vbscript)
  • Re: CSVDE Importing
    ... > specify the log file path via the -j option. ... CSVDE imports can only be used to create new users ... Unfortunately, the syntax is a bit more involved than a simple csv file, ... which makes it a little more difficult to manage its content via script. ...
    (microsoft.public.windows.server.scripting)
  • Re: CSVDE Importing
    ... > specify the log file path via the -j option. ... CSVDE imports can only be used to create new users ... Unfortunately, the syntax is a bit more involved than a simple csv file, ... which makes it a little more difficult to manage its content via script. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Recurse issue - 2nd try
    ... My script simply stops short. ... However, when I view the log file, it is missing several files. ... > is your script hanging or does it complete but somehow misses some folders? ... >> Dim sLogFile ...
    (microsoft.public.scripting.vbscript)