开发者

How can I use multiple PHP header content types on the same page? is this possible?

Is it possible to use multiple header types in one document? For example:开发者_运维知识库

header("Content-type: image/jpeg");
header('Content-Type: text/html; charset=utf-8');

returns the whole page as text/html... while

header('Content-Type: text/html; charset=utf-8');
header("Content-type: image/jpeg");

Returns the whole page as an image.... How can I use both types of content on the same page? I'm using ob_start() at the top and ob_end_flush() at the beginning.


You can't. But what you can do is something like this in your HTML:

<img src="my_img.php" />

Of course my_img.php would be a PHP file that has a header("Content-type: image/jpeg"); line, and outputs your image.


No, a resource has just one media type. HTTP allows multipart messages that can contain multiple entities. But that’s not widely supported by today’s browser.


The entire point of content-type is to tell the browser how to handle a request. What would you expect a browser to do if you're sending text/html and image/jpeg? It has to be one or the other, it can't be both. text/html is kind of a catch-all.


I do find it somewhat bizarre when people question why someone would want to do what they are asking in the initial question.

Having spent a couple of hours working on alternative solutions, which didn't work, for a similar requirement to the OP, this is precisely what I needed.

The usage I require is for displaying multiple images that are read from above the root and therefore not normally available via an IMG tag, so the solution that Erik has posted does the job beautifully and elegantly.

<img src="my_img.php" />

You are able, as usual, to pass parameters to this wrapper file, in order that it finds the relevant image, creates a thumbnail version or whatever.

In summary, excellent answer and thank you.


Be careful about what parameters you are passing to the wrapper file, in order that it finds the relevant image, and ONLY the relevant image, not /etc/passwd or any other file.

It's best to hard code the base dir in the script and don't accept any hierarchy in the input. It's probably best to not use real filenames at all and do the lookup in a mapping table (in a db, array in your script or wherever)


You can't use both at the same time, but you could optionally use one or the other based on a query parameter or similar.

In regards to using both types of content in one page - in a normal HTML page with <img> elements, for example, those are separate HTTP requests so they have their own Content-Type header.

Not exactly sure what you're trying to achieve here...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜