好的,我有以下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变量传入,即ThankYou.php?file=sample.pdf
这段代码在我的情况下工作得很好。也许它会对你有帮助。这是主页:
<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中你的代码只下载文件,那么你的主页将不会被重新加载。因此,处理表单的脚本将工作。
添加header()
功能,在下载成功后重定向您的页面
header( "refresh:5;url=yourlocation.php" );
资源链接(在这里)
使用html或javascript重定向,因为它工作,即使头已经发送。如果下载的是示例文件,则回显此命令。
。
HTMLecho "<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>';