PHP - determine how many bytes sent over http

后端 未结 2 1694

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

相关标签:
2条回答
  • 2020-12-03 06:43

    Take a look at the ignore_user_abort and connection_abort function.

    0 讨论(0)
  • 2020-12-03 06:53

    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);
    
    0 讨论(0)
提交回复
热议问题