7

我有一个 php 脚本,它允许用户下载带有下载简历的大文件。

这工作正常,但我已将 php 代码的下载速度限制为每个连接 200kb/s。

因此,Mozilla 的 Firefox 下载管理器只建立一个连接,速度约为 200kb/s,但 Free-Download-Manager 或 JDownloader 建立了 2 到 4 个连接,所以下载速度为 (200Kb/s * 2 或 4) = 400 到800kb/秒。

我怎样才能停止这种情况并只允许每个用户使用一个连接来下载此文件?

4

1 回答 1

7

A. I think the first thing for you is to disable Content-Range ..

14.16 Content-Range

The Content-Range entity-header is sent with a partial entity-body to specify where in the full entity-body the partial body should be applied. Range units are defined in section 3.12.

Download manager can download a single fine in 2 or multiple connections because of range .. if you disable this both download resume or multiple connections can not be made on a single file. they would make every request to the file start from the beginning

Example

LoadModule headers_module modules/mod_headers.so 
Header set Accept-Ranges none 
RequestHeader unset Range

You should also look at 14.35.1 Byte Ranges

B. Introduce Download Sessions .

You can generate a uniqid id for each download and serve it via PHP page. If the download is still active or has been requested before you just exist the page

Example

$realFile = "test.pdf";
$fakeFile = uniqid("file");

$uniqid = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
if (empty($uniqid) || strlen($uniqid) < 20 || !ctype_xdigit($uniqid)) {
    die("Die! Die! Die! Stolen URL");
}
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);

$runtime = (int) $memcache->get($uniqid);

if ($runtime) {
    die("Die! Die! Die! You Multiple Down loader");
} else {
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Content-Transfer-Encoding: binary");
    header("Content-disposition: attachment; filename=$fakeFile.pdf"); //
    header('Content-type: application/pdf');
    header("Content-length: " . filesize($realFile));
    readfile($realFile);
    $memcache->set($uniqid, 1);
}

Simple Client

$url = "a.php?id=" . bin2hex(mcrypt_create_iv(30, MCRYPT_DEV_URANDOM));
printf("<a href='%s'>Download Here</a>",$url);

It would output something like

<a href='a.php?id=aed621be9d43b0349fcc0b942e84216bf5cd34bcae9b0e33b9d913cccd6e'>Download Here</a>

You also need map each id to a particular file ...

于 2012-10-15T19:34:24.937 回答