Here is trimmed down version of what I have for a file download function. In
my version the filename is generated by the action params, you would have
customize that part to your setup. Content Type would also have to be
modified, as they are getting pdf files this setup.
Maybe it will help.
public function downloadAction () {
// path to file
$timage = "/generate/real/path/to/file/";
// break file up
$tp = pathinfo($timage);
// disable mvx layout
Zend_Layout::getMvcInstance()->disableLayout();
// disable this view
$this->_helper->viewRenderer->setNoRender();
// set headers
header("Content-Type: application/pdf");
header("Content-disposition: attachment;
filename=".$tp['basename'].";");
header("Content-Length: ".filesize($timage));
header('Content-Transfer-Encoding: Binary');
header('Accept-Ranges: bytes');
header('ETag: "'.md5($timage).'"');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
//send file data
// or print file_get_contents($timage);
readfile_chunked($timage);
}
// pulled from php.net comments
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 15*1024; // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
unset($buffer);
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile()
does.
}
return $status;
}
-----Original Message-----
From: j5 [mailto:[EMAIL PROTECTED]
Sent: Thursday, November 20, 2008 2:30 PM
To: [email protected]
Subject: Re: [fw-general] Action Helper To Send Files?
Should these callback functions be added to the action controller? Or added
as new methods of a class extending ContextSwitch?
Matthew Weier O'Phinney-3 wrote:
>
> -- Sudheer <[EMAIL PROTECTED]> wrote (on Thursday, 20
> November 2008, 06:48 PM +0530):
>> I have few questions:
>>
>> 1. Would it make sense to write a PDF file sender Action Helper for ZF?
>> I was wondering whether we could add this feature to contextSwitch
>> Action Helper.
>> 2. Or would it make more sense to write a generic file sender Action
>> Helper with various adapters - PDF, CSV, Text, etc? 3. Does something
>> like this exist already?
>>
>> The Action Helper would perform the following:
>> 1. Disable layout
>> 2. Disable view
>> 3. Set appropriate headers
>> 4. Send the file
>
> ContextSwitch allows you to specify arbitrary callbacks to call
> per-context; I'd simply use it and create a callback that performs
> these tasks.
>
> --
> Matthew Weier O'Phinney
> Software Architect | [EMAIL PROTECTED]
> Zend Framework | http://framework.zend.com/
>
>
--
View this message in context:
http://www.nabble.com/Action-Helper-To-Send-Files--tp20600931p20608355.html
Sent from the Zend Framework mailing list archive at Nabble.com.