2012-08-03 30 views
1

第1个链接到第2页第2页提供使用下面的代码下载:PHP后刷新提供下载

header("Content-disposition: attachment; filename= '$filename'"); 
header('Content-type: application/pdf'); 
readfile($file); 
header("location: mainpage.php"); 

的结果是第1页上的用户“岿然不动”,但供应下载。

我该如何进行设置,以便用户保留在第1页上,但在下载完成后刷新。

我不知道JavaScript,所以我希望纯PHP的解决方案。

回答

1

在我看来,我不会认为你必然需要刷新第1页可言。您应该能够通过page1中的链接强制下载。请看下图:

与page1.php中的链接

<a href="http://www.domain.com/page2.php?pdf=name-of-pdf">Download PDF</a> 

使page2.php

$filename = $_GET['pdf'] . '.pdf'; 

header('Content-type: application/pdf'); 
header("Content-disposition: attachment; filename= '$filename'"); 
header("location: $filename"); 

这将允许下载开始,而你留在第1页。

希望这是你的想法。

+0

所以你解释我到底在做什么。我想要的,其实是相反的。我需要这个确切的效果,但下载后我需要刷新页面,以便它显示“下载PDF”为“别的东西” – user187680 2012-08-04 00:37:46

0

您可以通过$ _SERVER ['HTTP_REFERER']来检查引用者是什么。所以,你应该能够把这个你page1.php中:

if($_SERVER['HTTP_REFERER'] == page2.php) { 
    echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.yourdomain.com/page1.php\">"; 
exit(); 
} 

这样,你检查你的访问者是从使page2.php就要来了,如果是这样,你只解析元标记刷新浏览器。当它刷新时,它不会再刷新,因为HTTP_REFERER现在是page1.php。

2

以前不知道,但它只是一个很好的HTTP标题,我们大多数人已经从HTML中知道它:Refresh

只需添加以下header电话:

header('Refresh: 0; url=http://stackoverflow.com/'); 
+0

这有什么用? – user187680 2012-08-03 23:53:13

+0

开始下载后,指定的网址将自动加载 - 无需HTML技巧。这不是你想要的吗? – 2012-08-03 23:56:49

+0

只需将地址替换为用户当前的地址即可。 – 2012-08-03 23:57:25

0

不知道你是否曾经得到这个回答,但我有同样的问题, 这里是我的解决方案 的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 
}); 

}); 

的HTML具有动态链接的大名单

<div data-itemid="<?php echo $row_Files['FID']; ?> " > 
<a class="downloadLink" href="PLC_FILES/WaterCheckOut.php?FID=<?php echo $row_Files['FID']; ?> " target=""><img src="PLCImages/download.fw.png"></a> 
</div> 

极化文件watergetstatus.php

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); 
} 

下载文件链接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); 

我简化了我的代码,并切出很多,所以它可能失去了一些东西,但是这是我使用的通用框架工作,它为我的作品

希望这可以帮助某人,因为那里没有太多