【问题标题】:How to download a file then redirect to another page in php?如何下载文件然后重定向到php中的另一个页面?
【发布时间】:2013-04-08 06:21:35
【问题描述】:

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

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

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

【问题讨论】:

  • "pdf/".$file。这不是一个好主意。如果我通过../../../../../srv/http/index.php怎么办?
  • OK 重定向适用于 mvc,你可以使用 header("location");通过 javascript 或 window.location。
  • @Coderanonymous – 不,他们不能。
  • @Coderanonymous — 因为“这是您要的(要下载的文件)”和“您要的在那边”是不相容的陈述。

标签: php html download


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这段代码在我的情况下运行良好。也许它会帮助你。 这是主页:

    <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 你的代码只下载文件,那么你的主页将不会被重新加载。所以处理表单的脚本会起作用。

    【讨论】:

    • 这还不错,但是您的 1 秒超时假定下载将在单击按钮后最多 1 秒内开始。但可能需要更长的时间。在这种情况下,不会下载任何内容,因为页面会在下载开始之前重定向到新位置。
    【解决方案3】:

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

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

    资源链接(here)

    【讨论】:

    • HTTP 标头必须位于响应正文之前(这是文件所在的位置)。 (Location 标头也需要一个绝对 URI)
    • 我已经尝试过了,但它也没有重定向。这是因为头函数必须在任何回声之前,并且在while循环中有回声......
    • @Quentin 哦,我看到标头已经声明了.. 但是在使用header( "refresh:5;url=wherever.php" );发送标头后仍然可以重定向
    • @你确定是朋友吗?..hmm。甚至打开输出缓冲?
    • 是的,我确定。输出缓冲无济于事,因为我在输出任何内容之前设置了标题。
    【解决方案4】:

    使用 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 攻击。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2011-06-02
    • 1970-01-01
    • 2014-02-22
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多