保护下载

发布于 2024-10-26 03:33:09 字数 549 浏览 4 评论 0原文

我只是在下面测试这个下载脚本。下载工作正常,但下载的 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>

I was just testing this download script below. The downloading works fine but the downloaded zip or rar archive is always corrupt and cannot be opened. I tested it on local development server as well as my hosting account.

I am just trying to learn how this works but I don't really understand it.

All help is appreciated!

Test Code:

<?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>

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

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

发布评论

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

评论(2

愿与i 2024-11-02 03:33:09

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

<?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>";
}

It mostly probable that you have something appended/prepended to the file. Try to use buffering and cleaning.

<?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>";
}
吹泡泡o 2024-11-02 03:33:09

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

Have you configured your webserver (Apache in particular) to load mod_xsendfile? Without that module installed, your script essentially does nothing.

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