好的,我有以下 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/