detect bytes written on abort
- From: Shailesh Humbad <humbads@xxxxxxxxxxxxxxx>
- Date: Thu, 05 Jan 2006 21:04:55 -0500
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-executionfunction 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 .
- Follow-Ups:
- Re: detect bytes written on abort
- From: Chung Leong
- Re: detect bytes written on abort
- From: Gordon Burditt
- Re: detect bytes written on abort
- Prev by Date: concurrency, locks, multi-user app.
- Next by Date: Re: detect bytes written on abort
- Previous by thread: concurrency, locks, multi-user app.
- Next by thread: Re: detect bytes written on abort
- Index(es):
Relevant Pages
|