Title: Download files with zend contextSwitch() Action Helper
Tags: zend-framework;controller;action;helper;context-switch
Question: everybody. I'm stack with similar thing: i have to provide a download file capability with Zend Framework... Several hours of googling don't help me with this...
So here is my Controller code (note: i'm a beginner):
//callback
public function sendFile()
readfile(APPLICATION_PATH . "/../public/pdf/10.pdf");
public function init()
$this-&gt;_helper-&gt;contextSwitch()
-&gt;addContext('file', array(
'headers' =&gt; array(
'Content-Type'          =&gt; 'application/pdf',
'Content-disposition'   =&gt; 'attachment; filename="10.pdf"'),
'callbacks' =&gt; array(
'init'  =&gt; '_sendFile'
)
))
-&gt;addActionContext('download', 'file')
-&gt;setAutoJsonSerialization(false)
-&gt;initContext();
// ...
public function downloadAction()
PS: I find this download files with zend framework but i want to do this Zend way.
Thank you all


Here is another answer: You could try.
public function init()
$this-&gt;_helper-&gt;contextSwitch()
-&gt;addContext('file'))
-&gt;addActionContext('download', 'file')
-&gt;setAutoJsonSerialization(false)
-&gt;initContext();
// ...
public function downloadAction()
if ($this-&gt;_helper-&gt;contextSwitch()-&gt;getCurrentContext() == 'file') {
$this-&gt;getResponse()
-&gt;setHeader('Content-type', 'application/pdf; charset=binary')
-&gt;setHeader('Content-Disposition', 'attachment; filename="10.pdf"')
-&gt;setHeader('Content-length', filesize(APPLICATION_PATH . "/../public/pdf/10.pdf"))
-&gt;setHeader('Cache-control', 'private');
readfile(APPLICATION_PATH . "/../public/pdf/10.pdf");
$this-&gt;getResponse()-&gt;sendResponse();
} else {
throw new Zend_Controller_Action_Exception('File not found', 404);
You must also set the parameter format to file either as a POST or GET variable in the calling page, for example.
<a href="http://yourdomain.com/path/to/controller/format/file"&gt;
If your file is in your public documents folder you could just simply link to it directly.
<a href="http://yourdomain.com/pdf/10.pdf"&gt;
and not bother with the above PHP/ZF code.
I hope this helps.
Kind regards
Comment for this answer: Big thank you, @Garry ! You approach successfully works! But why zend dev people advice to do it via callbacks? [contextSwitch() callbacks](http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch) Many thanks for quick answer)
Comment for this answer: And more one thing: if i do it via own callback ($this->sendFile()) - it start to output raw pdf file body to browsers window... Why it doing that. Where is problem?
Comment for this answer: maybe i missed filesize() to the Content-length header and it shows me raw data... H-m-m. But anyway, thank you, very much, @Garry !
Comment for this answer: @andrew I have made some changs to the code above, this works for me.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:05/29/11', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<zend-framework><controller><action><helper><context-switch>', 'Title': 'Download files with zend contextSwitch() Action Helper', 'ViewCount': 4314, 'id': 6167336, 'user_AboutMe': '<p>web developer</p>\n', 'user_AccountId': 405785.0, 'user_DisplayName': 'andrew', 'user_Reputation': 379.0, 'user_Views': 15.0, 'user_id': 774902.0}}