因爲你可以推定製標題之前不能輸出任何東西,我會建議使用JS重定向到下載,這通常讓你在同一頁上(只要你只是處理壓縮的內容,沒有別的)。
所以,試試這個:
$download = 'example.zip';
echo '<head> <script type="text/javascript"> function doRedirect(){window.location = "'.$download.'"}</script>
</head><html><script type="text/javascript"> doRedirect() </script> <...Example web page...</html>';
或者,如果你需要它的計時器:
echo '<head> <script type="text/javascript"> function doRedirect(){window.location = "'.$download.'"}</script>
</head><html><script type="text/javascript">
setTimeout(doRedirect(),1000);//wait one second</script> <...Example web page...</html>';
編輯:
如果你想隱藏的文件路徑,我會建議製作一個下載腳本,JS將重定向到。
所以基本上,要做你正在做的事情,然後用JS指出它。像這樣:
下載。PHP:
//use an ID or something that links to the file and get it using the GET method (url params)
$downloadID = $_GET['id'];
//work out the download path from the ID here and put it in $download
if ($downloadID === 662)
{
$download = 'example.zip';//...
}
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=$download');
readfile($download);
,然後在主HTML文件,使用JS指向它,用正確的ID:
<head> <script type="text/javascript"> function doRedirect(){window.location = "Download.php?id=662"}</script>
</head><html><script type="text/javascript"> doRedirect() </script> <...Example web page...</html>
打印內容後無法發送自定義標題。 – 2012-08-10 03:04:28
@navnav謝謝。你推薦我做什麼? – 2012-08-10 03:05:56
啊,所以你不希望他們看到文件路徑? – 2012-08-10 03:19:16