限制并行/同步下载 - 如何知道下载是否被取消? [英] Limiting Parallel/Simultaneous Downloads - How to know if download was cancelled?

查看:143
本文介绍了限制并行/同步下载 - 如何知道下载是否被取消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的文件上传服务,用PHP编写,它还包括一个脚本,通过发送有限大小的数据包来控制下载速度,当用户请求从该站点下载。

I have a simple file upload service, written out in PHP, which also includes a script that controls download speeds by sending limited-sized packets when a user requests a download from this site.

如果不是高级成员,我想实施一个系统来将并行/同步下载限制为每个用户1个。在上面的下载脚本中,我可以使用MySQL数据库来存储一个记录:(1)用户ID; (2)文件ID; (3)下载开始时; (4)当发送最后一个数据包时,每完成一次数据包更新(如果DL速度限制在150 kB /秒,则每150 kB之后更新此记录等)。

I want to implement a system to limit parallel/simultaneous downloads to 1 per user if they are not premium members. In the download script above, I can use a MySQL database to store a record that has: (1) the user ID; (2) the file ID; (3) when the download was initiated; and (4) when the last packet was sent, which is updated each time this is done (if DL speed is limited to 150 kB/sec, then after every 150 kB, this record is updated, etc.).

但是,到目前为止,数据库记录只有在下载成功完成后才会被删除 - 在脚本结束后,下载完成后,记录将从表:

However, thus far, the database record will only be deleted once the download has successfully completed — at the end of the script, after the download has been fully served, the record is deleted from the table:

insert DB record;
while (download is being served) {
    serve packet of data;
    update DB record with current date/time;
}
// Download is now complete
delete DB record;

我如何能够检测下载何时被取消?如果现有的下载记录超过X分钟/小时,我将只需要一个Cron工作(或类似的东西)来检测?还有还有什么我可以做的,我失踪了吗?

How will I be able to detect when a download has been cancelled? Would I just have to have a Cron job (or something similar) detect if an existing download record is more than X minutes/hours old? Or is there something else I can do that I'm missing?

我希望我已经解释得很好了。我不认为发布特定的代码是必需的;我更感兴趣的是物流如何/是否可以完成。如果需要具体,我将很乐意提供。

I hope I've explained this well enough. I don't think posting specific code is required; I'm interested more in the logistics of how/whether this can be done. If specific is needed, I will gladly provide it.

注意:我知道如何检测文件是否已成功下载;我需要知道如何检测是否被取消,中止或以其他方式停止(而不是暂停)。 这将有助于停止并行下载,以及防止用户取消下载#1并尝试启动下载#2的情况,只发现该网站声称他仍在下载文件#1。 strong>

NOTE: I know how to detect if a file was successfully downloaded; I need to know how to detect if it was cancelled, aborted, or otherwise stopped (and not just paused). This will be useful in stopping parallel downloads, as well as preventing a situation where the user cancels Download #1 and tries to initiate Download #2, only to find that the site claims he is still downloading file #1.

编辑:您可以在这里找到我的下载脚本: http ://codetidy.com/1319/ - 它已经支持多部分下载并下载恢复。

You can find my download script here: http://codetidy.com/1319/ — it already supports multi-part downloads and download resuming.

推荐答案

<?php

class DownloadObserver
{
  protected $file;
  public function __construct($file) {
    $this->file = $file;
  }

  public function send() {
    // -> note in DB you've started
    readfile($this->file);
  }

  public function __destruct() {
    // download is done, either completed or aborted
    $aborted = connection_aborted();
    // -> note in DB
  }
}

$dl = new DownloadObserver("/tmp/whatever");
$dl->send();

应该可以正常工作。不需要shutdown_function或任何时髦的自建连接观察。

should work just fine. No need for a shutdown_function or any funky self-built connection observation.

这篇关于限制并行/同步下载 - 如何知道下载是否被取消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆