好的,我有以下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);
?>
Run Code Online (Sandbox Code Playgroud)
想法是,如果它是示例文件,则将其下载,然后重定向到“谢谢”页面。但是,如果是付费下载,则此文件仅下载文件,但不执行任何操作(因为它们来自的页面是购买的下载文件链接的列表,因此需要保留在该页面上)。
但是,此页面正在执行的操作是下载文件,但没有重定向。我究竟做错了什么?我该如何解决?
小智 7
这段代码在我的情况下工作得很好。也许它会帮助你。它是主页:
<form action="/download.php" method="post">
<button type="submit">Download</button>
</form>
Run Code Online (Sandbox Code Playgroud)
主页中的这个脚本:
$(document).on('submit', 'form', function() {
setTimeout(function() {
window.location = "/thanks.html";
}, 1000);
});
Run Code Online (Sandbox Code Playgroud)
您必须在 dowload.php 中编写用于下载的 php 代码。如果在 download.php 中您的代码只下载文件,那么您的主页将不会重新加载。所以处理表单的脚本将起作用。
小智 5
最好的选择是将您的页面顺序改变一点。设置一个“感谢您下载此示例”页面,并对其进行设置以进行javascript刷新,从而实际将您带到下载页面。由于是下载而不是新页面,因此谢谢页面仍然存在。如果浏览器上没有javascript,请在实际文件上添加一个链接。
您可以为每个文件创建一个页面,但是最好的选择是将文件名作为get var传入,即 ThankYou.php?file=sample.pdf