7

好的,我有以下 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);
    ?>

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

然而,这个页面正在做的是下载文件但不重定向。我究竟做错了什么?我该如何解决?

4

4 回答 4

8

最好的办法是稍微改变你的页面顺序。设置一个“感谢您下载此示例”页面的页面,并将其设置为执行 JavaScript 刷新,实际上将您带到下载。因为它是一个下载页面,而不是一个新页面,所以感谢页面仍然存在。如果他们在浏览器上没有 javascript,请放置一个指向实际文件的链接。

您可以为每个文件创建一个页面,但最好的办法是将文件名作为 get var 传递,即ThankYou.php?file=sample.pdf

于 2013-04-08T16:47:53.627 回答
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);
});

您必须在 dowload.php 中编写 php 代码以供下载。如果在 download.php 你的代码只下载文件,那么你的主页将不会被重新加载。所以处理表单的脚本将起作用。

于 2018-04-13T12:05:27.307 回答
2

添加header()下载成功后重定向页面的功能

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

资源链接(这里

于 2013-04-08T06:23:26.283 回答
-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>';
于 2013-04-08T06:44:47.267 回答