I used two methods depending on the contents of the file. For documents such as Excel documents, I usually use this approach:
$this->getResponse()->clearHttpHeaders(); $this->getResponse()->setHttpHeaders('Content-Description','File Transer'); $this->getResponse()->setHttpHeaders('Content-Type','application/vnd.ms-excel'); //this would be based on your file $this->getResponse()->setHttpHeaders('Content-Disposition','attachment;filename='.$filename); //$filename is name of file on server $this->getResponse()->setHttpHeaders('Pragma',''); $this->getResponse()->setHttpHeaders('Cache-Control',''); $this->getResponse()->sendHttpHeaders(); $error_reporting = error_reporting(0); $this->renderText($some_data->save('php://output')); //in this case the $some_data was a PHPExcel writer object but anything that can be saved to a [php://output][1] should work eg fwrite() error_reporting($error_reporting); return sfView::NONE
Disabling and enabling error_reporting involves using PHPExcel to write to the stream.
The other method I used uses the sendContent() sfResponse method. An example of this use:
$this->getResponse()->clearHttpheaders(); $this->getResponse()->setHttpHeader('Content-Description','File Transfer'); $this->getResponse()->setHttpHeader('Cache-Control', 'public, must-revalidate, max-age=0'); $this->getResponse()->setHttpHeader('Pragma: public',true); $this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary'); $this->getResponse()->setHttpHeader('Content-length',filesize($filename))
Both approaches work, and you don't need a template to render the content / file.
Note. Edited to add to $this->getResponse()->sendHttpHeaders() before setting up and sending content
source share