tags:

views:

241

answers:

3

Hello,

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

+1  A: 

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

http://stackoverflow.com/questions/1507985/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);
nash
A: 

i still having problem with the code...

Asrul
I've udpated my answer to include example code. Please have a look.
nash
A: 

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
where should i put the code ?
Asrul
@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