【问题标题】:Protecting Downloads保护下载
【发布时间】:2011-03-24 00:33:52
【问题描述】:

我只是在下面测试这个下载脚本。下载工作正常,但下载的 zip 或 rar 存档总是损坏且无法打开。我在本地开发服务器以及我的托管帐户上对其进行了测试。

我只是想了解它是如何工作的,但我并不真正理解它。

感谢所有帮助!

测试代码:

<?php
$is_logged_in = 1;
$path_to_file = 'downloads/tes.zip';
$file_name = 'test.zip';

if ($is_logged_in == 1)
{
    header("X-Sendfile: $path_to_file");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$file_name\"");
    exit;
}
?>

<h1>Permission denied</h1>
<p>Please Login first!</p>

【问题讨论】:

  • 在文本编辑器中打开文件。你看到了什么?
  • 你不发送任何数据,是吗?
  • 您是否按照该代码的建议安装了mod_xsendfile
  • @icktoofay,不,对不起,我猜这两个服务器都没有安装这个。我只是想如果没有安装代码会刹车。我没有收到任何错误 - 下载工作正常。

标签: php download x-sendfile


【解决方案1】:

您很可能在文件中附加/前置了一些内容。尝试使用buffering 和清洁。

<?php
ob_start();
$is_logged_in = 1;
$path_to_file = 'downloads/tes.zip';
$file_name = 'test.zip';

if ($is_logged_in == 1)
{
    $fp = fopen($path_to_file, 'rb');

    if(is_resource($fp))
    {
            ob_clean();
            header("Content-Type: application/force-download");
            header("Content-Length: " . filesize($path_to_file));
            header("Cache-Control: max_age=0");
            header("Content-Disposition: attachment; filename=\"$file_name\"");
            header("Pragma: public");
            fpassthru($fp);
            die;
    }
} else {
    echo "<h1>Permission denied</h1>";
    echo "<p>Please Login first!</p>";
}

【讨论】:

  • 有没有使用 mod_xsendfile 的替代方法。是否可以在不安装模块的情况下做到这一点?
  • 谢谢,保留我写的标题,它解决了 IE 下载的一些问题。
【解决方案2】:

您是否已将您的网络服务器(尤其是 Apache)配置为加载 mod_xsendfile?如果没有安装该模块,您的脚本基本上什么都不做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多