下面我的代码以块的形式输出一个大文件(以节省内存),每次请求下载文件时,计数器都会增加。我已经在多个浏览器中对此进行了测试,它似乎按预期工作。但是我有一个用户记录了一个文件的 578 次下载,而另一个用户报告了超过 9000 次下载的文件。我的带宽费用并不表明发生了这种情况。
我唯一能想到的是,如果他们使用可以建立多个连接的下载加速器。
你还能看到其他可能性吗?
if (isset($_GET['gf']) && $eligible)
{
$_GET['file'] = isset($_GET['file']) ? $_GET['file'] : FILE_SOURCE;
if (is_valid_file($_GET['file']))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($_GET['file']));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($_GET['file']));
echo readfile_chunked($_GET['file'], true, $auth);
exit;
}
}
// credit: http://teddy.fr/blog/how-serve-big-files-through-php
define('CHUNK_SIZE', 1024*1024);
function readfile_chunked($filename, $retbytes = true, $auth = false) {
$download_column = false;
switch($filename)
{
case FILE_SOURCE:
$download_column = 'manual_downloads';
break;
case FILE_WIN_INSTALLER:
$download_column = 'win_downloads';
break;
case FILE_MAC_INSTALLER:
$download_column = 'mac_downloads';
break;
case FILE_LINUX_32_INSTALLER:
$download_column = 'linux_32_downloads';
break;
case FILE_LINUX_64_INSTALLER:
$download_column = 'linux_64_downloads';
break;
}
if ($download_column && $auth)
{
$auth = mysql_real_escape_string($auth);
$query = "UPDATE payments SET $download_column = $download_column + 1 WHERE auth = '$auth'";
mysql_query($query);
}
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, CHUNK_SIZE);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt;
}
return $status;
}