X-Sendfile controller helper (w/ Nginx support)
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 104 105106 107 108 109 110 | <?php /** * X-Sendfile controller helper * * @copyright Copyright (c) 2008-2009 Pro Soft Resources USA Inc. (http://www.prosoftpeople.com) * @author Rolando Espinoza La fuente (rho@prosoftpeople.com) * @license http://www.opensource.org/licenses/bsd-license.php New BSD License * @version $Id$ */ class Zend_Controller_Action_Helper_Xsendfile extends Zend_Controller_Action_Helper_Abstract { /** * @var bool Use Nginx specific header */ protected $_isNginx = false; /** * @var bool Wheter to force download. Default true. */ protected $_forceDownload = true; /** * Send files through X-Sendfile feature * * @param string $path File path. * @param string $filename File name. Optional. * @return $this */ public function xsendfile($path, $mime = null, $filename = null) { // set filename if not given if (null === $filename) { $filename = basename($path); } $response = $this->getResponse(); // send mime info if (null !== $mime) { $response->setHeader('Content-Type', $mime); } // send file as attachment if ($this->forceDownload()) { $contentDisposition = 'attachment; filename=' . $filename; } else { $contentDisposition = 'inline; filename=' . $filename; } // if server is Nginx use different header if ($this->isNginx()) { $headerName = 'X-Accel-Redirect'; } else { $headerName = 'X-Sendfile'; } // set response headers $response->setHeader('Content-Disposition', $contentDisposition) ->setHeader($headerName, $path); // Done. Webserver will send the file return $this; } /** * Force download flag * * @param boolean Optional. * @return boolean */ public function forceDownload($flag = null) { if ($flag !== null) { $this->_forceDownload = (bool) $flag; } return $this->_forceDownload; } /** * Nginx web server flag * * TODO: Support internal paths * * @param boolean Optional. * @return boolean */ public function isNginx($flag = null) { if ($flag !== null) { $this->_isNginx = (bool) $flag; } return $this->_isNginx; } /** * Direct pattern * * @param string $path File path. * @param string $filename File name. Optional. * @return $this */ public function direct($path, $mime = null, $filename = null) { return $this->xsendfile($path, $mime, $filename); } } |
Comments
Great idea!
The problem with this helper is that if webserver does not support X-sendfile the client will receive nothing. But that will be resolved using your suggestion, sendfile adapters.
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
To use this controller helper your web server must support X-Sendfile. Nginx (X-Accel-Redirect) and Lighttpd already support it, to use with apache must install mod_xsendfile.
If you have problems with apache, try this directies:
XSendFile on
XSendFileAllowAbove on
= Usage
public function downloadAction()
{
$file = '/path/to/file.ext';
$mime = 'image/png';
// disable output
$this->_helper->layout->disableLayout();
$this->_helper->viewRender->setNoRender(true);
// if using nginx
//$this->_helper->Xsendfile->isNginx(true);
// not force download
//$This->_helper->Xsendfile->forceDownload(false);
// send file with custom name
//$this->_helper->xsendfile($file, 'application/pdf', 'report.pdf');
$this->_helper->xsendfile($file, $mime);
}
Snippet details
- Created:
-
Rho
- Edited:
-
Rho
- Revision Id:
- 26
- Edit Message:
- Initial Release
- Tags:
- controller helper sendfile download
- Comments:
- 2
- Views:
- 209
- Points:
- 0 (0 votes)
1 year ago
This is a nice implementation. Good work.
Have you considered creating a more general "sendfile" package where you could separate specific sendfile methods like X-Sendfile into unique adapters? Something like this would ensure that the logic in your controllers remains server-configuration independent while still giving you the flexibility to use this functionality if it is available.