【问题标题】:How to determine how many bytes sent over http如何确定通过 http 发送的字节数
【发布时间】:2009-11-10 17:19:44
【问题描述】:

我需要知道如何确定通过 http 发送了多少字节。

这是我到目前为止所做的。

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(); }

谁能帮帮我?

谢谢

【问题讨论】:

标签: php download


【解决方案1】:

看看这个问题的另一个帖子:

PHP - determine how many bytes sent over http

取自链接页面的代码示例(最初由 J. 发布,已修改以适合您的示例):

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

【讨论】:

    【解决方案2】:

    尝试使用readfile的返回值:

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

    【讨论】:

    • @Asrul:将readfile 调用中的代码替换为我的代码。但我不确定readfileignore_user_abort 是如何连接的。
    猜你喜欢
    • 2010-12-03
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多