1

下载完成后,我需要将下载状态设置为 0。我正在使用 x-sendfile,但完成下载后,不要设置状态。

我的代码:

header("X-Sendfile: $file");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . $name . '"');

mysql_query("UPDATE `downloads` SET `download_status` = '1' WHERE `id` = '" . $last_down . "'");

感谢帮助

4

1 回答 1

1

如果您使用 X-Sendfile,您无法检测到客户端何时完成下载文件。最简单的方法是通过脚本流文件。

$handle = fopen($filepath, 'rb');
if ($handle !== false) {
    @flock($handle, LOCK_SH);

    $size = filesize($filepath);
    $filename = basename($filepath);
    $old_max_execution_time = ini_get('max_execution_time');
    ini_set('max_execution_time', 0);
    $old_cache_limiter = session_cache_limiter();
    session_cache_limiter(false);

    if (ob_get_length()) ob_clean();
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Content-disposition: attachment; filename="'. $filename .'"');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Accept-Ranges: bytes');
    header('Cache-control: private');
    header('Pragma: private');
    set_time_limit(0);
    while (! feof($handle) && (connection_status() === 0)) {
        if (! $buffer = @fread($handle, $interval)) {
            // Error
            exit;
        }

        print($buffer);
        flush();
    }
    @flock($handle, LOCK_UN);
    @fclose($handle);
    ini_set('max_execution_time', $old_max_execution_time);
    session_cache_limiter($old_cache_limiter);

    // ----------------------------------------------------
    // Downloading complete!
    // Here you can update your table row in DB...
    // ----------------------------------------------------

    exit;
}
于 2014-05-17T23:17:16.590 回答