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

发布于 2024-12-09 11:47:32 字数 1382 浏览 0 评论 0原文

我遇到一个问题,一个字节的数据损坏了我的 Word 文档。当我从服务器下载时,它打开得很好,但是当我强制从网络下载时,文件会大一个字节并且已损坏。我一直在寻找“额外”的空间,直到我脸色发青。

奇怪的是,在 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);
}

I am having an issue with one byte of data corrupting my word document. It opens fine when I download from server, but when I force download from web the file is one byte larger and corrupted. Ive looked until I am blue in the face for the "extra" space.

Strangely enough on windows, if I click recover document comes out fine....

 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-12-16 11:47:32

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

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

After readfile($fileName . '.' . $this->_extension); add exit;.

If problem persists, try to add at begin of the function die( '.' );, run your script and look at page source if dot (.) is the first character.

对岸观火 2024-12-16 11:47:32

造成此类问题的最常见原因是 之前或 ?> 之前的空白字符。

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

Most common cause of this sort of problem by a long, long way is a whitespace character before <?php or after ?>.

Note that you probably don't actually need the closing ?> in a file that is pure PHP code, which helps avoid this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文