输出流损坏文档中的空格问题

时间:2011-10-13 21:03:07

标签: php http-headers

我遇到的问题是一个字节的数据损坏了我的word文档。当我从服务器下载时它打开很好,但是当我强制从web下载时,文件大一个字节并且已损坏。我一直看着,直到我脸上的蓝色为“额外”的空间。

在Windows上奇怪的是,如果我点击恢复文件就好了......

 public function DownloadDocx()
{
    $this->_extension = "docx";
    $args = func_get_args();
    $newName = trim($args[0]);
    ob_flush();
    if (!empty($newName)) {
        $fileName =$_SERVER['DOCUMENT_ROOT'] . '/wills/docs/' .$newName;
    } else {
        throw new Exception("Invalid document name");
    }
      if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
        header("Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        header("Content-Disposition: inline; filename=\"". $newName . '.' . $this->_extension."\"");
        header("Content-Length: ".filesize($fileName . '.' . $this->_extension));
     } else {
        header("Content-type: application/force-download");
        header("Content-Disposition: attachment; filename=\"". $newName . '.' . $this->_extension."\"");
        header("Content-Length: ".filesize($fileName . '.' . $this->_extension));
     }
     header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
     if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
      header("Cache-Control: no-cache");
      header("Pragma: no-cache");
     }

     readfile($fileName . '.' . $this->_extension);
}

2 个答案:

答案 0 :(得分:0)

长期以来,这类问题的最常见原因是<?php之前或?>之后的空白字符。

请注意,您可能实际上不需要在纯PHP代码的文件中关闭?>,这有助于避免此问题。

答案 1 :(得分:0)

readfile($fileName . '.' . $this->_extension);后添加exit;

如果问题仍然存在,请尝试在函数die( '.' );的开头添加,运行脚本并查看页面源,如果点(。)是第一个字符。

相关问题