php - 如何下载文件然后重定向到php中的另一个页面?

标签 php html download

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

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

但是,此页面正在执行的操作是下载文件但不重定向。我究竟做错了什么?我该如何解决?

最佳答案

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

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

关于php - 如何下载文件然后重定向到php中的另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872534/

相关文章:

php - PHP脚本的安全性,嵌入式或其他

android - 如何避免本地 Web 应用程序数据被删除?

android - 保存的图像覆盖了 SD 卡 android 中的先前图像

php - Facebook 标签提要 PHP cURL

php - 根据我自己的选择在 Mysql 数据库表中自动递增

html - Bootstrap 列布局

javascript - 更改已应用样式的 html 表格行颜色

java - 如何使用 Java 从 Google App Engine 创建下载链接

jquery - 使用按钮强制下载 .txt 文件

php - row_array() codeigniter 只返回第一行