How to calculate Content-Length for a file download within Kohana PHP?
Posted
by moritzd
on Stack Overflow
See other posts from Stack Overflow
or by moritzd
Published on 2010-04-05T17:07:35Z
Indexed on
2010/04/05
17:13 UTC
Read the original article
Hit count: 161
I'm trying to send a file from within a Kohana model to the browser, but as soon as I add a Content-Length header, the file doesn't start downloading right away.
Now the problem seems to be that Kohana is already outputting buffers. An ob_clean at the begin of the script doesn't help this though. Also adding ob_get_length() to the Content-Length isn't helping since this just returns 0. The getFileSize() function returns the right number: if I run the script outside of Kohana, it works.
I read that exit() still calls all destructors and it might be that something is outputted by Kohana afterwards, but I can't find out what exactly.
Hope someone can help me out here...
This is the piece of code I'm using:
public function download() {
header("Expires: ".gmdate("D, d M Y H:i:s",time()+(3600*7))." GMT\n");
header("Content-Type: ".$this->getFileType()."\n");
header("Content-Transfer-Encoding: binary\n");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",$this->getCreateTime()) . " GMT\n");
header("Content-Length: ".($this->getFileSize()+ob_get_length().";\n");
header('Content-Disposition: attachment; filename="'.basename($this->getFileName())."\"\n\n");
ob_end_flush();
readfile($this->getFilePath());
exit();
}
© Stack Overflow or respective owner