在CakePHP中将标题设置为内容图像 [英] Setting the header to be content image in CakePHP
问题描述
我在控制器中写一个动作,在某种情况下,我想直接输出原始图像数据,并且想要设置标题内容类型。但是我认为标题已经由CakePHP早期设置了(我将设置为false)。
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!
推荐答案
如前所述,当 render
false
。当心,任何代码做一个echo将发送头(除非你使用输出缓冲)。这包括来自PHP(警告等)的邮件。
As said before, CakePHP does not send headers when render
is false
. 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:
function send_file_using_plain_php($filename) {
// Avoids hard to understand error-messages
if (!file_exists($filename)) {
throw RuntimeException("File $filename not found");
}
$fileinfo = new finfo(FILEINFO_MIME);
$mime_type = $fileinfo->file($filename);
// The function above also returns the charset, if you don't want that:
$mime_type = reset(explode(";", $mime_type));
// gets last element of an array
header("Content-Type: $mime_type");
header("Content-Length: ".filesize($filename));
readfile($filename);
}
使用X-Sendfile并让Web服务器提供文件
Use X-Sendfile and have the Webserver serve the file
// This was only tested with nginx
function send_file_using_x_sendfile($filename) {
// Avoids hard to understand error-messages
if (!file_exists($filename)) {
throw RuntimeException("File $filename not found");
}
$fileinfo = new finfo(FILEINFO_MIME);
$mime_type = $fileinfo->file($filename);
// The function above also returns the charset, if you don't want that:
$mime_type = reset(explode(";", $mime_type));
// gets last element of an array
header("Content-Type: $mime_type");
// The slash makes it absolute (to the document root of your server)
// For apache and lighttp use:
header("X-Sendfile: /$filename");
// or for nginx: header("X-Accel-Redirect: /$filename");
}
第一个函数占用一个PHP进程/数据正在发送,并且不支持范围请求或其他高级HTTP功能。
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.
使用X-Sendfile你可以得到所有的,但你需要知道哪个网络服务器正在运行,甚至可能需要更改配置。特别是在使用lighttp或nginx时,这在性能上是有利的,因为这些网络服务器极其擅长从磁盘提供静态文件。
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.
这两个函数都支持文档 - web服务器的根。在nginx中有所谓的内部位置(http://wiki.nginx.org/HttpCoreModule#internal)。这些可以与 X-Accel-Redirect
-Header一起使用。甚至可以进行速率限制,请查看 http://wiki.nginx.org/XSendfile 。
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.
如果您使用apache,则有mod_xsendfile,它实现第二个函数所需的功能。
If you use apache, there is mod_xsendfile, which implements the feature needed by the second function.
这篇关于在CakePHP中将标题设置为内容图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!