PHP中的文件下载问题及内容处置:附件

PHP中的文件下载问题及内容处置:附件,php,zend-framework,download,attachment,content-disposition,Php,Zend Framework,Download,Attachment,Content Disposition,我的网页上有一个链接,可以下载我在服务器上生成的.CSV文件。下载代码如下: //open/save dialog box header('Content-Disposition: attachment; filename="inventoryData.csv"'); //content type header('Content-type: application/excel'); //read from server and write to buffer readfile('spreadsh

我的网页上有一个链接,可以下载我在服务器上生成的.CSV文件。下载代码如下:

//open/save dialog box
header('Content-Disposition: attachment; filename="inventoryData.csv"');
//content type
header('Content-type: application/excel');
//read from server and write to buffer
readfile('spreadsheet/inventory.csv');
当我在服务器上打开文件时,它看起来很好。但是,当我通过对话框下载文件时,它将网页的HTML代码预挂起到.csv文件中

你知道为什么会这样吗

试试这个:

header("Content-type: application/octet-stream");
header("Content-disposition:attachment; filename=inventoryData.csv");

这应该能奏效

  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: private",false);
  header("Content-Type: application/octet-stream");
  header("Content-Disposition: attachment; filename=\"inventoryData.csv\";" );
  header("Content-Transfer-Encoding: binary");

如果这段代码是在控制器操作中,我认为这是因为您正在使用ZF,那么您需要禁用布局和视图渲染器,因为它将尝试渲染视图

尝试:


$this->\u helper->layout()->disableLayout()阻止渲染布局脚本(假设您使用布局),以及
$this->\u helper->viewrenter->setNoRender(true)
告诉视图渲染器不要渲染控制器操作的视图脚本,该操作可能包含一些HTML或空格。

您是否确保以前没有其他代码被回显?
应用程序/excel
应该是。您可能在php.ini中启用了HTML错误。im为什么被否决?我用这段代码在我的网站上下载csv文件已经快一年了,它从来没有给我一个错误。使用不同的、不太有用的内容类型如何阻止HTML数据被预先添加到输出中?我想问这与这个问题有什么关系。那么,你认为这个问题的答案是什么?我认为这个问题在目前的形式下是无法回答的,patyx7的评论说明了OP应该如何开始调试它。更改HTTP头将如何阻止HTML数据被预先添加到HTTP正文中?啊,我怎么会错过这一点?主要道具给你!
public function downloadAction()
{
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    //...

    //open/save dialog box
    header('Content-Disposition: attachment; filename="inventoryData.csv"');
    //content type
    header('Content-type: application/excel');
    //read from server and write to buffer
    readfile('spreadsheet/inventory.csv');
}