因为你可以推定制标题之前不能输出任何东西,我会建议使用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