2

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

Asrul
  • 21
  • 1

2 Answers2

1

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);
Community
  • 1
  • 1
nash
  • 2,181
  • 15
  • 16
0

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
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • @Asrul: Replace your code from the `readfile` call up to the end with mine. But I’m not sure how `readfile` is connected with `ignore_user_abort`. – Gumbo Nov 09 '09 at 15:40