【问题标题】:Create dynamic, expireable and resumable download link using php, apache使用 php、apache 创建动态、可过期和可恢复的下载链接
【发布时间】:2012-06-14 15:11:59
【问题描述】:

问题详情如下:

1) 我想创建动态(基于 IP)下载链接。所以用户不能用同一个下载链接下载不同IP的文件。

2) 在开始实际下载之前,我想使用 php 记录这个下载请求并执行一些检查(验证 http 引荐来源网址)以允许用户下载实际文件。

3) 我还希望下载文件是可恢复的,并且可以使用下载管理器下载(具有多个下载实例)。还想限制每次下载允许的最大实例数。

4) 文件大小可能超过 200 MB。

所以,我正在考虑的解决方案是使用用户 ip 的 md5 哈希创建下载链接。例如。 http://yourdomain.com/download.php?ip_hash=hash-of-the-ip&file=file-to-download

这只是一个例子,但我们也可以使用 htaccess 创建一个很好的链接。

接下来我该怎么做?我尝试使用

header("Content-Type: $ctype");
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=\"$fileName\"");
readfile($file);

但是使用这个下载不会对最终用户保持恢复。

用这种方法发送大文件可以吗?

在做了一些研究后,我了解到使用这种方式的最终用户的 .exe 文件会损坏。

【问题讨论】:

  • 您需要确保对哈希进行加盐处理,否则用户只需将其替换为新 IP 的 md5 即可从其他地方下载。
  • 如果用户没有静态IP地址怎么办?或许实现一个用户帐户系统会更好。

标签: php apache security download


【解决方案1】:

在做了更多研究之后,我找到了我的问题的答案。我只是想我也应该与你们分享。

正如 rambo 所说,我们可以使用 apache 服务器的 mod_xsendfile 模块。如果它被禁用,我们需要启用它。

如果你的 apache 没有这个模块,这里是下载模块文件的链接。它适用于几乎所有版本的 apache,并且适用于 x32 和 x64。
https://github.com/nmaier/mod_xsendfile

在完成所有自定义验证后,您可以使用以下代码使用此 apache 模块发送文件。

<?php
//We want to force a download box with the filename hello.txt
header('Content-Disposition: attachment;filename=hello.txt');

//File is located at data/hello.txt
header('X-Sendfile: data/hello.txt');
?>

希望对大家有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2016-02-13
    • 2020-07-11
    • 2016-08-01
    相关资源
    最近更新 更多