php文件下载的输出缓冲

php文件下载的输出缓冲,php,http-headers,download,Php,Http Headers,Download,我可以这样做吗 ob_start(); header("Content-Type: application/msword"); header("Content-Disposition: attachment; filename=".$title); header("Content-Length: ".$size); header("Content-Transfer-Encoding: binary"); readfile($path); ob_end_flush(); 目前,我正在下载正确命名

我可以这样做吗

ob_start();
header("Content-Type: application/msword");
header("Content-Disposition: attachment; filename=".$title);
header("Content-Length: ".$size);
header("Content-Transfer-Encoding: binary");
readfile($path);
ob_end_flush();
目前,我正在下载正确命名的文件,但该文件的内容是包含上述代码的php的html输出

我已验证$path是否正确。错误的美元大小会导致这种情况发生吗? 我还尝试在ob_start()之后直接使用ob_flush()

编辑:当前代码如下:

if (file_exists($path)){
    ob_start();
    header("Content-Type: application/msword");
    header("Content-Disposition: attachment; filename=".$title);
    header("Content-Length: ".filesize($path));
    header("Content-Transfer-Encoding: binary");
    readfile($path);
    ob_end_flush();
    die();
    } else die ("Not Found");
上述代码导致了相同的问题。文件已下载,命名正确,显然存在,大小正确,但文件内容是页面的html输出

提前谢谢

我刚刚注意到这是在下载的文件的末尾(错误): “int(14)内容”


但我并不是有意将其输出到代码中的任何地方。

也许一些调试可能会有所帮助:

if (file_exists($path)) {
  ob_start();
  header("Content-Type: application/msword");
  header("Content-Disposition: attachment; filename=".$title);
  header("Content-Length: ".filesize($path));
  header("Content-Transfer-Encoding: binary");
  readfile($path);
  ob_end_flush();
  die(); // To not send the HTML afterwards
} else die("Not found");

请记住,ob_start会导致readfile读取内存中的整个文件,这会导致大文件出现内存问题。还要确保$size正确,
filesize($path)将是完美的,但我无法在您的代码中看到它。