I am writing an action in a controller where in a certain case, I want to output raw image data directly, and want to set the header content-type appropriate. However I think the header is already being set earlier by CakePHP (I am setting render to be false).
Is there a way to get around this? Thanks!
As said before, CakePHP does not send headers when
renderisfalse. Beware though, that any code doing an ‘echo’ will send headers (except you are using output-buffering). This includes messages from PHP (warnings etc.).Sending the file can be done in numerous ways, but there are two basic ways:
Send the file using plain PHP
Use X-Sendfile and have the Webserver serve the file
The first function occupies one PHP-process / thread while the data is being send and supports no Range-Requests or other advanced HTTP-features. This should therefore only be used with small files, or on very small sites.
Using X-Sendfile you get all that, but you need to know which webserver is running and maybe even a change to the configuration is needed. Especially when using lighttp or nginx this really pays off performance-wise, because these webservers are extremly good at serving static files from disk.
Both functions support files not in the document-root of the webserver. In nginx there are so called “internal locations” (http://wiki.nginx.org/HttpCoreModule#internal). These can be used with the
X-Accel-Redirect-Header. Even rate-throtteling is possible, have a look at http://wiki.nginx.org/XSendfile.If you use apache, there is mod_xsendfile, which implements the feature needed by the second function.