首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP限制下载速度

使用PHP限制下载速度
EN

Stack Overflow用户
提问于 2012-01-29 07:46:07
回答 3查看 828关注 0票数 3

我在这里有这段代码,但是我想限制用户下载的速度,我如何在这段代码中实现这一点;

代码语言:javascript
复制
header("Content-type: application/force-download");
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: ".filesize("uploads/$filename"));
    header("Content-disposition: attachment; filename=\"$origname");
    readfile("uploads/$filename");

谢谢!

这就是我尝试过的;

代码语言:javascript
复制
$download_rate = 100;

$origname = get_file_name($file);

代码语言:javascript
复制
header("Content-type: application/force-download");
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: ".filesize("uploads/$filename"));
    header("Content-disposition: attachment; filename=\'$origname'");

    $local_file = "uploads/$origname";

// flush content flush();

代码语言:javascript
复制
// open file stream
$file = fopen($local_file, "r");

while (!feof($file)) {

    // send the current file part to the browser
    print fread($file, round($download_rate * 1024));

    // flush the content to the browser
    flush();

    // sleep one second
    sleep(1);
}

// close file stream
fclose($file);

为什么这个不起作用?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-29 07:56:51

如果你已经尝试了@mugur建议的方法,那么在其他地方可能会有问题。例如,在您的代码片段中,似乎有一个缺失的转义引号:

header("Content-disposition:附件;filename=\"$origname");

我想应该是:

header("Content-disposition:附件;filename=\"$origname\"");

票数 1
EN

Stack Overflow用户

发布于 2012-01-29 07:48:19

希望这能对http://www.jonasjohn.de/snippets/php/dl-speed-limit.htm有所帮助

票数 1
EN

Stack Overflow用户

发布于 2012-01-29 08:52:16

在while fread中,只计算字节和时间戳。然后使用usleep添加一个小小的停顿。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9049634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档