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

20,723

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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)

Solution 5

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.

Share:
20,723
Jon
Author by

Jon

Updated on July 09, 2022

Comments

  • Jon
    Jon almost 2 years

    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.

    • mmattax
      mmattax about 14 years
      Why would you even want to do this? Not possible...
    • Brian Stinar
      Brian Stinar about 11 years
      I also wanted to do this because I did not understand about HTTP headers, before researching them. There is a php routine that I find useful which depend on header("Content-type: image/jpeg"); I would like to incorporate it into other routines. Erik's suggestion will exactly work for me.
  • Jon
    Jon about 14 years
    hmm... ok thanks. I'm trying to dynamically generate thumbnails on the fly with a function that returns an image resource using imagejpg($img) after running imagesampleresize etc etc... Problem is I can't seem to do it 'on' the page by calling a function.. .either the page displays text, or it displays an image... can't seem to output an image resource ON a text/html page.
  • Jon
    Jon about 14 years
    This is helpful... i think my answer is to use <img src="my_img.php" /> where my_img.php is essentially a function that outputs an image.