输出流中的空格损坏文档的问题
我遇到一个问题,一个字节的数据损坏了我的 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
readfile($fileName . '.' . $this->_extension);
之后添加exit;
。如果问题仍然存在,请尝试在函数开头添加
die( '.' );
,运行脚本并查看页面源代码(如果点 (.) 是第一个字符)。After
readfile($fileName . '.' . $this->_extension);
addexit;
.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.造成此类问题的最常见原因是
之前或
?>
之前的空白字符。请注意,您可能实际上不需要纯 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.