php服务下载后刷新
第1页链接到第2页。第2页使用以下代码提供下载:php服务下载后刷新,php,javascript,header,download,refresh,Php,Javascript,Header,Download,Refresh,第1页链接到第2页。第2页使用以下代码提供下载: header("Content-disposition: attachment; filename= '$filename'"); header('Content-type: application/pdf'); readfile($file); header("location: mainpage.php"); 结果是用户“停留”在第1页上,但获得下载服务 如何设置,以便用户保留在第1页上,但在下载服务完成后,页面会刷新 我不懂javascr
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/pdf');
readfile($file);
header("location: mainpage.php");
结果是用户“停留”在第1页上,但获得下载服务
如何设置,以便用户保留在第1页上,但在下载服务完成后,页面会刷新
我不懂javascript,所以我希望有一个纯PHP的解决方案。您可以通过$\u服务器['HTTP\u referer']检查referer是什么。因此,您应该能够在您的page1.php中输入以下内容:
if($_SERVER['HTTP_REFERER'] == page2.php) {
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.yourdomain.com/page1.php\">";
exit();
}
if($\u服务器['HTTP\u REFERER']==page2.php){
回声“;
退出();
}
通过这种方式,您可以检查访问者是否来自page2.php,如果来自page2.php,则只解析刷新浏览器的元标记。当它被刷新时,它不会再次刷新,因为HTTP_REFERER现在是page1.php 以前不知道这一点,但这只是一个很好的HTTP头,我们大多数人已经从HTML中知道了:
Refresh
只需添加以下标题
调用:
header('Refresh: 0; url=http://stackoverflow.com/');
在我看来,我认为你根本不需要刷新第1页。您应该能够通过第1页内的链接强制下载。见下文: 带有链接的Page1.php
<a href="http://www.domain.com/page2.php?pdf=name-of-pdf">Download PDF</a>
这将允许在您停留在第1页时开始下载
希望这就是你的想法。不确定你是否得到过答案,但我也有同样的问题, 这是我的解决办法 AJAX jquery
$(function(){
$("#itemList").on("click", "a.downloadLink", function(){ //this binds a click event handler on the itemList container that will listen out for any a with class of downloadLink inside it being clicked
var link = $(this);
var item = link.parent();
var forId = item.data("itemid");
var started = new Date(); //the alternative to tracking time elapsed is to just use a simple counter you increment - "poll 5 times" etc. if your doing .5 second intervals, then 5 times = 2.5 seconds for example. Time elapsed may result in less polls, if a poll takes a long time to return, for example. Use whichever approach feels better.
var maxTime = 5000; //5 seconds
function poll(){
$.ajax({
type: "POST",
url: "Watergetstatus.php",
data: {FID: forId}, //this will be turned into a request for page1?forId=1&oldValue=2 - I expect it in this example to return a json-encoded response of {"changed":true|false, "newHtml":"replacementContent on success"}
datatype :'json',
success: function(data){
if (data.changed=true)
{
window.location.reload(true);
}
else {
var elapsed = (new Date())-started;
//window.location.reload(true);
if (elapsed <= maxTime) setTimeout(poll, 500); // Poll again in .5 seconds
//else you can assume the link didn't open / work / the database never changed etc - handle or ignore as needed
}
},
error: function() {
alert("borken"); //the request to the server bombed out; up to you if you want to simply re-queue for another try like above until the expiry time or if you want to show an error or just simply ignore it.
}
});
}
setTimeout(poll, 500); //wait 0.5 seconds before polling
});
});
下载文件链接watercheckout.php
if ( $row_files['Status']==1 ) {
$file_path = $row_files['FileName'];;
$path_parts = pathinfo($file_path);
$file_name = $path_parts['basename'];
$file_ext = $path_parts['extension'];
$content_types = array(
"exe" => "application/octet-stream",
"zip" => "application/zip",
"mp3" => "audio/mpeg",
"mpg" => "video/mpeg",
"avi" => "video/x-msvideo",
);
$ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default;
$file = $row_files['FileName'];
$path = "Historical/".date('Y-m-d-His');
$newfile = $path."_".$file;
$today = date('Y-m-d H:i:s');
header('Content-disposition: attachment; filename='.$file);
header("Content-Type: " . $ctype);
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
mysql_select_db($database_PLC, $PLC);
mysql_query("UPDATE files SET Status = '2'");
ob_clean();
flush();
readfile($file);
rename($file, $newfile);
我简化了我的代码,删掉了很多内容,所以它可能遗漏了一些东西,但这是我使用的通用框架,对我来说很有用
希望这对其他人有所帮助,因为下载页面顶部没有太多内容----------------------
//----------------- TOP OF DOWNLOAD_PAGE.PHP ----------------------
$download_code = mysql_real_escape_string(urldecode($_GET['code']));
$download = mysql_real_escape_string(urldecode($_GET['download']));
$self = $_SERVER["PHP_SELF"]."?code=$download_code";
//refresh page after download...
echo"
<script type=\"text/javascript\">
function downloadRedirect(){
var redirect_url = \"$self\";
setTimeout(\"DoTheRedirect('\"+redirect_url+\"')\",
parseInt(0.5*1000));
}
function DoTheRedirect(url) { window.location=url; }
</script>
";
...
$filepath = "/var/www/vhosts/YOUR_DOMAIN.com/digital_downloads/";
if (isset($_GET['download'])) {
$file = $_GET['download'];
if (file_exists($filepath.$download) &&
is_readable($filepath.$download) && (preg_match('/\.zip$/',$download) || preg_match('/\.zipx$/',$download) )) {
...
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$download."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$download));
ob_end_flush();
readfile($filepath.$download);
}//end if file_exists
}//end if isset
//----------------- BOTTOM HALF OF DOWNLOAD_PAGE.PHP ----------------------
//link generated to download and call JS to refresh page
echo "<a href=\"".$_SERVER["PHP_SELF"]."?download=$download_id.zipx&code=$download_code\" target=\"_top\" onclick=\"javascript:downloadRedirect()\">Click to Download</a>
$download\u code=mysql\u real\u escape\u string(urldecode($\u GET['code']);
$download=mysql\u real\u escape\u字符串(urldecode($\u GET['download']);
$self=$\u SERVER[“PHP\u self”]。“?code=$download\u code”;
//下载后刷新页面。。。
回声“
函数downloadRedirect(){
var redirect\u url=\“$self\”;
setTimeout(\“dotherDirect('\“+重定向\\u url+\”)\”,
parseInt(0.5*1000));
}
函数dotherDirect(url){window.location=url;}
";
...
$filepath=“/var/www/vhosts/YOUR_DOMAIN.com/digital_downloads/”;
如果(isset($_GET['download'])){
$file=$\u获取['download'];
如果(文件_存在($filepath.$download)&&
可读($filepath.$download)&(preg\u匹配('/\.zip$/',$download)| preg\u匹配('/\.zipx$/',$download))){
...
标题(“Pragma:public”);
标题(“到期日:0”);
标头(“缓存控制:必须重新验证,后检查=0,前检查=0”);
标头(“缓存控制:公共”);
标题(“内容描述:文件传输”);
标题(“内容类型:应用程序/八位字节流”);
标题(“内容处置:附件;文件名=\”.$download.\”);
标题(“内容传输编码:二进制”);
标题(“内容长度:“.filesize($filepath.$download));
ob_end_flush();
readfile($filepath.$download);
}//如果文件_存在,则结束
}//如果已设置,则结束
//-----------------下载页面的下半部分----------------------
//生成链接下载并调用JS刷新页面
回声“
开始下载后,指定的url将自动加载-无需HTML技巧。这不是您想要的吗?只需将该地址替换为用户的当前地址。因此,您准确地解释了我的操作。我想要的实际上是相反的。我需要这个确切的效果,但在下载后,我需要刷新页面,以便将“下载pdf”显示为“其他内容”。一个好的答案需要的不仅仅是代码。需要解释它如何回答最初的问题。
mysql_select_db($database_PLC, $PLC);
$query_files = "SELECT * FROM files WHERE FID = '{$_POST['FID']}'";
$files = mysql_query($query_files, $PLC) or die(mysql_error());
$row_files = mysql_fetch_assoc($files);
$totalRows_files = mysql_num_rows($files);
if($row_files['Status'] ==2)
{
$data= array("changed"=>true);
echo json_encode($data);
}
else
{
$data= array("changed"=>false);
echo json_encode($data);
}
if ( $row_files['Status']==1 ) {
$file_path = $row_files['FileName'];;
$path_parts = pathinfo($file_path);
$file_name = $path_parts['basename'];
$file_ext = $path_parts['extension'];
$content_types = array(
"exe" => "application/octet-stream",
"zip" => "application/zip",
"mp3" => "audio/mpeg",
"mpg" => "video/mpeg",
"avi" => "video/x-msvideo",
);
$ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default;
$file = $row_files['FileName'];
$path = "Historical/".date('Y-m-d-His');
$newfile = $path."_".$file;
$today = date('Y-m-d H:i:s');
header('Content-disposition: attachment; filename='.$file);
header("Content-Type: " . $ctype);
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
mysql_select_db($database_PLC, $PLC);
mysql_query("UPDATE files SET Status = '2'");
ob_clean();
flush();
readfile($file);
rename($file, $newfile);
//----------------- TOP OF DOWNLOAD_PAGE.PHP ----------------------
$download_code = mysql_real_escape_string(urldecode($_GET['code']));
$download = mysql_real_escape_string(urldecode($_GET['download']));
$self = $_SERVER["PHP_SELF"]."?code=$download_code";
//refresh page after download...
echo"
<script type=\"text/javascript\">
function downloadRedirect(){
var redirect_url = \"$self\";
setTimeout(\"DoTheRedirect('\"+redirect_url+\"')\",
parseInt(0.5*1000));
}
function DoTheRedirect(url) { window.location=url; }
</script>
";
...
$filepath = "/var/www/vhosts/YOUR_DOMAIN.com/digital_downloads/";
if (isset($_GET['download'])) {
$file = $_GET['download'];
if (file_exists($filepath.$download) &&
is_readable($filepath.$download) && (preg_match('/\.zip$/',$download) || preg_match('/\.zipx$/',$download) )) {
...
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$download."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$download));
ob_end_flush();
readfile($filepath.$download);
}//end if file_exists
}//end if isset
//----------------- BOTTOM HALF OF DOWNLOAD_PAGE.PHP ----------------------
//link generated to download and call JS to refresh page
echo "<a href=\"".$_SERVER["PHP_SELF"]."?download=$download_id.zipx&code=$download_code\" target=\"_top\" onclick=\"javascript:downloadRedirect()\">Click to Download</a>