php标题不下载

时间:2014-04-23 11:15:17

标签: php force-download

我试图制作一个强制下载"通过PHP,但我得到的只是奇怪的符号。

我尝试添加标题(" application / typeofmyfile")和标题(" Content-Disposition")......以及来自此处的许多其他帖子中的许多其他帖子,但是没有

我有这个:

$f_location = "path/myfile.nl2pkg";
$f_name = "myfile.nl2pkg";


header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$f_name); //Here also tried basename($f_name) but nothing
header("Content-Length: ".filesize($f_location));    
header("Content-Transfer-Encoding: binary");

我这样做是因为当用户点击"下载"按钮,用ajax我将他发送到页面强制下载,在数据库中放入一些数据,然后刷新主页。

这是我第一次使用标题,而对于我读过的内容,我不知道我是否做错了。

1 个答案:

答案 0 :(得分:0)

您的标题代码似乎没问题。问题似乎在其他地方。

要调试代码,请使用headers_sent确定是否已发送标头。您还可以使用headers_list查看已发送的标头。

if (headers_sent()) {
  var_dump(headers_list());
  die('Headers have already been sent');
  exit;
}

如果在定义标题之前使用此权限,则可能会找到问题的根源。