如何在PHP中下载文件并重定向到另一个页面?

8

好的,我有以下php文件:

    <?php
        header("Content-Type: application/octet-stream");

    $file = $_GET["file"];
    header("Content-Disposition: attachment; filename=" . urlencode($file));   
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");            
    header("Content-Length: " . filesize($file));
    $file = "pdf/".$file;
    flush(); // this doesn't really matter.
    $fp = fopen($file, "r");
    while (!feof($fp)) {
        echo fread($fp, 65536);
        flush(); // this is essential for large downloads
    }

    if ($file="pdf/sampleFile.pdf") {
    ?>
<html>
<head>
    <title>Best Recipes Ever!</title>
    <link rel="stylesheet" href="style/style.css" />
</head>

<body>
    <div id="header">
        <h1>Best Recipes Ever!</h1>
    </div>
    <div id="page">
        <h1>Thank You For Your Download!</h1>
        <p>I sincerely hope that you enjoy this free recipe! If satisfied, please feel free to return and purchase some of my recipes. Trust me, you won't regret it!</p>
    </div>
    </body>
    </html>
    <?php 
    }

    fclose($fp);
    ?>

这个想法是,如果是示例文件,它会下载文件,然后重定向到感谢页面。但是,如果是付费下载,则该文件仅下载文件,但不执行任何操作(因为它们来自的页面是已购买的下载文件链接列表,所以它需要留在该页面上)。

然而,这个页面正在下载文件,但没有重定向。我做错了什么?我该如何解决它?


2
"pdf/".$file 不是一个好主意。如果我传递 ../../../../../srv/http/index.php 会怎么样? - Blender
1
可能是PHP生成下载文件然后重定向的重复问题。 - Quentin
1
在MVC中,可以使用header("location")或者通过JavaScript使用window.location来进行重定向。 - Coder anonymous
1
@Coderanonymous - 不,他们不能。 - Quentin
1
@Coderanonymous - 因为“这是你要求的(一个要下载的文件)”和“你要求的在那里”这两个陈述是不兼容的。 - Quentin
显示剩余2条评论
4个回答

9
最好的方法是略微调整您的页面顺序。设置一个“感谢您下载此示例”的页面,并将其设置为执行JavaScript刷新,实际上会带您到下载页面。由于这是一个下载而不是一个新页面,所以感谢页面仍然会存在。如果浏览器没有 JavaScript,请放置指向实际文件的链接。
您可以为每个文件创建一个页面,但最好的方法是将文件名作为 get 变量传递,例如 ThankYou.php?file=sample.pdf

可以在目标页面上使用以下JS脚本:window.location = 'https://google.com'; - Omar Shishani

7
这段代码在我的情况下运行良好。也许它能帮到你。 这是主页面:
<form action="/download.php" method="post">
  <button type="submit">Download</button>
</form>

这是主页上的脚本:

$(document).on('submit', 'form', function() {
  setTimeout(function() {
    window.location = "/thanks.html";
  }, 1000);
});

您必须在download.php文件中编写PHP代码来进行下载。如果在download.php文件中的代码仅下载文件,则您的主页面将不会重新加载。因此,处理表单的脚本将会起作用。


1
这不错,但你的1秒超时假设下载将在按钮被点击后最多1秒内开始。但可能需要更长时间。在这种情况下,由于页面在下载开始之前将重定向到新位置,因此不会下载任何内容。 - Max

2

在下载成功后,添加一个header()函数来重定向您的页面。

header( "refresh:5;url=yourlocation.php" );

资源链接(在此)


1
HTTP头必须在响应体之前出现(文件也必须在此处)。 (另外,“Location”头部需要一个绝对URI) - Quentin
1
我已经尝试过了,但它也没有重定向。这是因为头函数必须在任何回声之前,而在 while 循环中有一个回声... - ByronArn
1
@Quentin 哦,我明白了,标头已经被声明了... 但是在使用 header(“refresh:5;url=wherever.php”); 发送标头后重定向仍然有可能吗? - Jhonathan H.
1
你确定吗?嗯,即使打开输出缓冲区也不行吗? - Jhonathan H.
1
是的,我确定。输出缓冲区不会有帮助,因为我在输出任何内容之前设置了标头。 - Quentin
哦,我明白了,谢谢提供信息。现在会考虑其他可能的解决方案。 - Jhonathan H.

-1
使用HTML或JavaScript重定向,因为即使头部已发送,它仍然有效。如果下载的是示例文件,只需输出此内容。
例如:
HTML
echo "<meta http-equiv = 'Refresh' content = '2; url =./redirectpage.php'/>"; // change redirectpage.php to where you want to redirect.

JavaScript

echo '<script type="text/javascript">
          window.location.href="./redirectpage.php";
      </script>';

如果响应正在加载一个内联的HTML文档,它可能会起作用……但事实并非如此,它是一个PDF附件。 - Quentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接