Asrul
Asrul

Reputation: 21

How to determine how many bytes sent over http

I need to know how to determine how many bytes sent over http.

This is what I did so far.


ignore_user_abort(true);

header('Content-Type: application/octet-stream; name="file.pdf"');
header('Content-Disposition: attachment; filename="file.pdf"');
header('Accept-Ranges: bytes');
header('Pragma: no-cache');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-transfer-encoding: binary');
header('Content-length: ' . filesize('file/test.pdf'));
readfile('file/test.pdf');

    if (connection_aborted()) {
        $transfer_success = false;
        $bytes_transferred = ftell($handle);
        echo $bytes_transferred;
        die();
    }

Can anyone help me out ?

Thanks

Upvotes: 2

Views: 808

Answers (2)

nash
nash

Reputation: 2181

Take a look at this other post of the same question:

PHP - determine how many bytes sent over http

Code example taken from the linked page (originally posted by J., modified to fit your example):

ignore_user_abort(true);

$file_path = 'file/test.pdf';
$file_name = 'test.pdf';

header('Content-Type: application/octet-stream; name=' . $file_name );
header('Content-Disposition: attachment; filename="' . $file_name . '"');
header('Accept-Ranges: bytes');
header('Pragma: no-cache');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-transfer-encoding: binary');
header('Content-length: ' . $file_path);

$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);

Upvotes: 1

Gumbo
Gumbo

Reputation: 655239

Try to use the return value of readfile:

$bytes_transferred = readfile('file/test.pdf');
$transfer_success = ($bytes_transfered == filesize('file/test.pdf'));
if (!$transfer_success) {
    // download incomplete
}

Upvotes: 0

Related Questions