2013-02-04 58 views
5

我正在使用這個小推動器php文件來下載'exe'文件,以便用戶在填寫表單時保存。如何在用戶下載文件後顯示一個Thankyou頁面?

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 

用這種方法沒有鏈接到在線$文件位置的人跟隨,但由於某種原因被執行的代碼之前,我以後不能顯示感謝您網頁,甚至馬上。

我試過header(「Location:$ thankyouurl」);上面的代碼後立即,但沒有任何顯示,我已經嘗試在運行上面的代碼之前回應感謝頁面的HTML源代碼,但這導致exe文件被下載到實際崩潰瀏覽器的網頁。

任何建議???

回答

14

將用戶直接指向顯示您的感謝信息的靜態頁面。然後,使用JavaScript將window.location設置爲您的下載腳本。

如果您的下載頁面的標題設置正確,瀏覽器將開始下載文件,而您的用戶正在閱讀您的感謝信息。

確保顯示直接下載鏈接,以防用戶禁用JavaScript。

實施例:

的index.php

<a href="thankyou.php">Click here to download.</a> 

thankyou.php

<html> 
<head> 
    <script type="text/javascript"> 
     function startDownload() { 
      window.location = "/download.php"; 
     } 
    </script> 
</head> 
<body onload="startDownload();"> 
    <h1>Thank you!</h1> 
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php">direct link.</a></p> 
</body> 

的download.php

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 

更新

爲了傳遞到下載腳本index.php數據,你可以使用GET變量。到thankyou.php頁面的鏈接將變爲thankyou.php?download=12,其中下載是您獲取正確文件的ID。然後,您可以通過回它在您的標記,像這樣傳遞變量到您的下載腳本:

的index.php

<a href="thankyou.php?download=12">Click here to download.</a> 

thankyou.php

<html> 
<head> 
    <script type="text/javascript"> 
     function startDownload() { 
      window.location = "/download.php?file=<?=$_GET['download']?>"; 
     } 
    </script> 
</head> 
<body onload="startDownload();"> 
    <h1>Thank you!</h1> 
    <p>Your download will start in a moment. If it doesn't, use this <a href="download.php?file=<?=$_GET['download']?>">direct link.</a></p> 
</body> 

你然後在您的下載腳本中獲取該值,並以任何您喜歡的方式處理它。

+0

我覺得這是它。但是現在我如何將$ file的值傳遞給download.php。在我的原始版本中,我使用了表單發佈和多個值來創建$ file的值,並且這些值是由「包含」下載器文件僞傳遞的。我已經通過形式ost調用初始化php文件來將你的想法和我的想法結合起來,這個文件使用頭文件中的startedownload java腳本回應了你的頁面的新版本。現在我不能看到如何將js中的$ file值傳遞給php文件。 –

+0

@MarkSowards查看我的更新是否有幫助。 – thordarson

+0

謝謝你的證實。我正要試試這個。 –

0

不會在這個不太清楚,一會...

header('Location: thanks_page.php'); 

...不行,。如果放在readfile()之後?不確定,真的,只有我的想法!

+0

只是想知道,另一種選擇可能是包含頭('Location:....');之前readfile()作爲腳本可以繼續運行,即使在標題加載謝謝頁後,只是一個選項,不是100%肯定,但值得一試?可能在ob_clean()之前;也。 – Joe

0

你可以用一個「windows.open(‘URL下載文件’)」

實例打開「下載頁面」:

<script type="text/javascript"> 
    function openDownload() 
    { 
     window.open("http://domain.com/download.php?file=file.pdf"); 
     window.location = "http://domain.com/thanks.php"; 
    } 
</script> 

打開直接下載文件的窗​​口,這將在您選擇保存或取消時自動關閉,然後「父」窗口重定向到感謝頁面。

+0

有趣的想法,但它不工作。 – Sylca

0

使用你原來的方法,添加header('location: thank-you-page.php');到年底,出口一起,如下所示:

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 

header('location: thank-you-page.php'); 
exit; 
+0

Thankyou頁面是一個html,看起來readfile後面什麼都沒有發生。 –

+0

如果你想強制文件下載,你爲什麼使用'readfile'? – adamdehaven

相關問題