2012-05-10 39 views
-1

我的代碼是:重定向任何頁面在PHP

readfile('cls_ref.csv'); 
fclose($fp); 
$file_type = 'octet-stream'; 
$file_name = "cls_ref_" . date('m-d-Y H:i'); 
$file_ending = "csv"; 
header("Content-Type: application/$file_type; charset=utf-8"); 
header("Content-Disposition: attachment; filename=$file_name.$file_ending"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
header("Location:index.php"); 

我想下載到index.php後重定向卻是下載之前重定向。

我在文件的末尾寫了一個代碼header("Location:index.php");,但它在 下載文件之前重定向。

任何解決方案?

+0

我不認爲你可以,你要打印出文件中的PHP文件重定向。也許嘗試在您有下載鏈接的頁面上使用JavaScript重定向?或者重定向到另一個開始下載並使用元標記重定向的php頁面。 –

+0

在你的頁面上實現JQuery。 – Norse

+0

[PHP生成文件下載然後重定向]的可能重複(http://stackoverflow.com/questions/822707/php-generate-file-for-download-then-redirect) –

回答

-3

如何約10秒鐘後元刷新:

print "<meta http-equiv=\"refresh\" content=\"10;URL=index.php\">"; 
+0

這不工作!它在文件末尾的'.csv'文件中被寫入。 –

+2

好像他在輸出文件的內容。打印任何東西都會將其附加到文件中,從而毀壞文檔。 –

+0

抱歉,我忘記了這一點。 – Adam