如何下载文件,然后重定向到PHP中的另一个页面? [英] How to download a file then redirect to another page in php?
问题描述
好吧,我有以下php文件:
Okay, I have the following php file:
<?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);
?>
想法是,如果它的示例文件,它下载文件,然后重定向到一个感谢页面。但是,如果它是一个付费下载,该文件只下载文件,但不做任何事情(因为他们来自的页面是已购买的下载文件链接的列表,因此需要留在该页面上)。
The idea is, if its the sample file, it downloads the file then redirects to a thank you page. However, if its a paid-for download(s), this file only downloads the files, but does nothing (because the page they are coming from is a list of purchased download file links, so it needs to stay on that page).
然而,这个页面正在下载文件,但不重定向。我究竟做错了什么?如何解决?
What this page is doing, however, is downloading the file but doesn't redirect. What am I doing wrong? And how can I fix it?
推荐答案
最好的打算是让你的页面顺序有所变化。设置一个感谢您下载此示例页面的页面,并设置一个JavaScript刷新,实际上需要您进行下载。因为它是一个下载,而不是一个新的页面,谢谢页面仍然会在那里。如果他们在浏览器上没有JavaScript,请链接到实际文件。
Best bet is to turn your page order around a little bit. Set up a page that's a "thank you for downloading this sample" page, and have it set up to do a javascript refresh that actually takes you to the download. As it's a download, not a new page, the thank you page will still be there. In case they don't have javascript on the browser, put a link to the actual file.
您可以为每个文件设置一个页面,但最好的办法是将文件名作为get var传递,即 ThankYou.php?file = sample.pdf
You could have a page for each file, but best bet would be to pass the filename in as a get var, i.e. ThankYou.php?file=sample.pdf
这篇关于如何下载文件,然后重定向到PHP中的另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!