PHP - determine how many bytes sent over http

让人想犯罪 __ 提交于 2019-11-27 04:04:41

问题


Is it possible in PHP to get a count of the number of bytes transmitted to the client? For example, if I'm outputting a 10 MB file, is there a way to find out if all 10 MB were sent to the client, or to see if the client interrupted the transfer partway? I know Apache will log this afterwards, but I'd like to access the data in PHP.


回答1:


Take a look at the ignore_user_abort and connection_abort function.




回答2:


Here's what I ended up doing (thanks Gumbo):

ignore_user_abort(true);

$handle = fopen($file_path, 'r');
while ( ! feof($handle)) {
    echo fread($handle, 4096);
    if (connection_aborted()) {
        $transfer_success = false;
        $bytes_transferred = ftell($handle);
        break;
    }
}
fclose($handle);


来源:https://stackoverflow.com/questions/1507985/php-determine-how-many-bytes-sent-over-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!