|
Posted by Shailesh Humbad on 10/06/95 11:36
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
Navigation:
[Reply to this message]
|