2013-11-09 35 views
0

我有这样的代码PHP下载文件头回声

  [... page content] 
      header("Content-type: application/x-download"); 
      header("Content-Length: ".$filesize); 
      header("Content-Disposition: attachment; filename=".urlLastSeg($buttons['Torrent file(Torcache)'])); 
      header("Content-Transfer-Encoding: binary"); 
      echo $filecontent ; 

然后浏览器提示保存文件,但该文件包含一个连接在年底,而不是单独的二进制数据的二进制数据的页面的HTML。

回答

0

为什么你的底部有标题?它被称为标题,因为它们位于响应之上。

它打我,这应该引发文件下载。我想你错过了显示一些代码。工作示例是这样的(仅此而已()!):

<?php 

    $filename = '...'; 
    header("Content-type: application/x-download"); 
    header("Content-Length: ". filesize($filename)); 
    header("Content-Disposition: attachment; filename=". $filename; 
    header("Content-Transfer-Encoding: binary"); 
    readfile($filename); 
+0

好吧,我想显示一些页面,然后将文件推送给用户。如果标题处于开头,用户在点击下载链接后会看到空白的白色屏幕? – kurdt

+0

点击下载链接后,您需要了解[http](http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) – hek2mgl

+0

,它会加载包含信息的新页面'下载将立即开始',然后会出现浏览器'下载提示'给用户。 – kurdt

0

在PHP中,你需要任何其他输出前将报头。

+0

你确定吗?缓冲区操作呢? – kurdt

+0

如果你正在使用缓冲区,那么在所有其他地方之前都会发送标题。 –