Issue with streaming file from custom controller

Hey,

This may be more of a Zend Framework issue but I’m putting it here in case any of you have seen it. I’ve been tasked with providing functionality that allows a download link from our Omeka powered site. It should stream the content so that the url is not made available.

I’ve got it all working bar the actual streaming (well, small fib, I have it working - just not in the way I’d prefer).

In a custom controller action I have:

$response = Zend_Controller_Front::getInstance()->getResponse();
$response->setHeader('Content-Type', 'application/zip', true);
$response->setHeader('Content-Disposition', 'attachment; filename="' . $zip->name . '"', true);
$response->setHeader('Content-Length', filesize($zip->path), true);
readfile($zip->path);

This should work and follows all the documentation I can find on the internet about doing this from within a Zend 1 controller.

But I get this error

PHP message: PHP Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

Getting my debugger up and running tells me it bombs out at line 588 of Zend_Controller_Response_Abstract, which as you can see is a simple echo statement.

For now I’ve fixed the issue by completely dumping out of the Zend response pipeline with

header('Content-Type: application/zip', true);
header('Content-Disposition: attachment; filename="' . $zip->name . '"', true);
header('Content-Length: ' . filesize($zip->path), true);

$obLevel   = ob_get_level();
if ($obLevel > 0) {
   do {
       ob_get_clean();
       $obLevel = ob_get_level();
   } while ($obLevel > 0);
}

readfile($zip->path);
exit();

I’d rather not do this obviously as I imagine there a lot of fragile stuff I could be breaking, shutdown functions and the like.

Any ideas?

Thanks
Adam

I haven’t seen this particular problem before. It might be related to your PHP configuration or maybe a plugin or some custom code? It should only happen if you’re using a output buffering callback and to my knowledge the normal Zend controller stuff doesn’t, just a generic plain buffer.

Breaking out of the buffering might be the right call though… as it is with the first snippet you might as well just be using file_get_contents because Zend’s built in buffering is going to pull the whole file into a string anyway.