Re: detect bytes written on abort



Gordon Burditt wrote:
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?

What are you going to use that information for?

I am sending a large file to the client, and I want to record how many bytes are actually sent.

Bytes sent != bytes received. Why is there a problem with the
script aborting? Modem drops carrier? Spontaneous Windows reboot?
Browser crash?


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.

If one end of a TCP connection quietly goes away, you might be able to write 64k more bytes before you realize it has gone away.

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).

What do you intend to use this for? If you're trying to figure out where to restart sending the file, it won't be accurate.

Gordon L. Burditt

TCP is a reliable transport, meaning that at the application layer, one always know exactly how much data the client received, and this is always equal to how much was successfully sent. I don't care how many bytes were transferred by TCP in the data link layer.


I don't want to restart sending the file. I also do not care why the script aborted. Of course, it must be from a network/client abort, not a server reboot or such, because the script must finish executing. I only want to be able to track how many bytes were sent to the client, which equals the value that is eventually written to the server log file.

The reason I need it is because in this system, I want to be able to show the user how many bytes the server sent them. This will tell them how much data transfer they have used.

I need the status of bytes sent as soon as possible after the script completes or aborts. Thanks.
.




Relevant Pages