PHP 中限制下载速度

发布于 2025-01-02 21:18:33 字数 497 浏览 0 评论 0原文

我已经尝试过在谷歌搜索中找到的基本方法,甚至尝试自己编写一个,但是我一直遇到问题。好像是在服务器端下载内容什么的,然后推送给用户,就已经下载好了。它将打开下载页面,大约需要 10 秒的时间来下载,然后将文件完整地提供给用户,这使得它看起来像是没有下载。

我想知道是否有任何已编写的类来限制下载速度,或者我如何解决这个问题。

我目前有这个;

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

谢谢!

I have tried the basic ones found in a Google search and even tried to write one myself, however i keep getting a problem with it. It seems to download the content server-side or something and then push it to the user, which will already have been downloaded. It will open the download page and take around 10 seconds to download and then give the file to the user in full, which makes it look like its not downloading.

I was wondering if there are any classes that have been written to throttle download speeds, or how i can fix this problem.

I have this currently;

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金橙橙 2025-01-09 21:18:33
@set_time_limit(0); // don't abort if it takes to long
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize("uploads/".$filename));
header('Content-disposition: attachment; filename="'.$origname.'"');
$perSecond = 5; // 5 bytes per second

$file = fopen("uploads/".$filename, 'r');
while(!feof($file)) {
    echo fread($file, $perSecond);
    flush();
    sleep(1);
}

这将向用户发送一个下载速度受到限制的文件。它的工作原理基本上是这样的:

  • 打开一个文件
  • 循环,直到最后
  • echo X 字节
  • 将输出刷新到用户
  • 睡眠一秒钟。
@set_time_limit(0); // don't abort if it takes to long
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize("uploads/".$filename));
header('Content-disposition: attachment; filename="'.$origname.'"');
$perSecond = 5; // 5 bytes per second

$file = fopen("uploads/".$filename, 'r');
while(!feof($file)) {
    echo fread($file, $perSecond);
    flush();
    sleep(1);
}

This will send a file with throttled download speed to the user. It works basically like this:

  • Open a file
  • loop until we are at the end
  • echo X bytes
  • flush the output to the User
  • sleep for one second.
信仰 2025-01-09 21:18:33

您可能会发现我的 alpha 阶段 感兴趣的带宽项目。可能还需要做更多的工作,但已经有很多有趣的东西了。我认为它还没有 F/OSS 许可证;如果你想让我给它一个,请告诉我!

You might find my alpha-stage Bandwidth project of interest. Probably needs a bit more work, but there's plenty of interesting stuff already. I don't think it has a F/OSS license yet; ping me if you want me to give it one!

无可置疑 2025-01-09 21:18:33

我想知道是否有任何类已编写来限制下载速度

现在有: 带宽节流/带宽节流

use bandwidthThrottle\BandwidthThrottle;

$in  = fopen(__DIR__ . "/resources/video.mpg", "r");
$out = fopen("php://output", "w");

$throttle = new BandwidthThrottle();
$throttle->setRate(100, BandwidthThrottle::KIBIBYTES); // Set limit to 100KiB/s
$throttle->throttle($out);

stream_copy_to_stream($in, $out);

I was wondering if there are any classes that have been written to throttle download speeds

Now there is: bandwidth-throttle/bandwidth-throttle

use bandwidthThrottle\BandwidthThrottle;

$in  = fopen(__DIR__ . "/resources/video.mpg", "r");
$out = fopen("php://output", "w");

$throttle = new BandwidthThrottle();
$throttle->setRate(100, BandwidthThrottle::KIBIBYTES); // Set limit to 100KiB/s
$throttle->throttle($out);

stream_copy_to_stream($in, $out);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文