issue with whitespace in output stream corrupting document
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."\"开发者_JAVA百科");
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);
}
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.
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.
精彩评论