php 如何下载文件然后重定向到php中的另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15872534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download a file then redirect to another page in php?
提问by ByronArn
Okay, I have the following php file:
好的,我有以下 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);
?>
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?
然而,这个页面正在做的是下载文件但不重定向。我究竟做错了什么?我该如何解决?
回答by Andrew Leap
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.
最好的办法是稍微改变页面顺序。设置一个页面,即“感谢您下载此示例”页面,并将其设置为执行 javascript 刷新,实际将您带到下载。由于它是一个下载页面,而不是一个新页面,感谢页面仍然存在。如果他们在浏览器上没有 javascript,请放置一个指向实际文件的链接。
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
您可以为每个文件创建一个页面,但最好的办法是将文件名作为 get var 传入,即 ThankYou.php?file=sample.pdf
回答by Nurbol Sarsenbayev
This code worked in my situation well. Maybe it will help you. It is main page:
这段代码在我的情况下工作得很好。也许它会帮助你。它是主页:
<form action="/download.php" method="post">
<button type="submit">Download</button>
</form>
This script in main page:
主页中的这个脚本:
$(document).on('submit', 'form', function() {
setTimeout(function() {
window.location = "/thanks.html";
}, 1000);
});
You must write your php code for download in dowload.php. If in download.php your code only downloads file, then your main page will not be reloaded. So the script which handles form will work.
您必须在 dowload.php 中编写用于下载的 php 代码。如果在 download.php 中您的代码只下载文件,那么您的主页将不会重新加载。所以处理表单的脚本将起作用。
回答by Jhonathan H.
回答by TravellingGeek
Use html or javascript redirects, because it works even if headers were already sent. Just echo this if the download is a sample file.
使用 html 或 javascript 重定向,因为即使标题已经发送,它也能工作。如果下载的是示例文件,只需回显。
e.g.
例如
HTML
HTML
echo "<meta http-equiv = 'Refresh' content = '2; url =./redirectpage.php'/>"; // change redirectpage.php to where you want to redirect.
JavaScript
JavaScript
echo '<script type="text/javascript">
window.location.href="./redirectpage.php";
</script>';