How to Use Multiple PHP Header Content Types on the Same Page? Is This Possible

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

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.

Can I use multiple Content-type's on same page?

No, you can only have one Content-Type per page.

What you should do is simply create another page, that contains both an image (with src pointing to the code you showed) and the form you want to display, like this:

<html>
<body>
<div>
<img src="image.php" alt="image retreived from DB" />
</div>
<form>
<input type="text" name="foo" />
...
</form>
</body>
</html>

How can I pass multiple file extensions parameter in header?

If you want in downloading any type of file you can just use application/octet-stream.

header('Content-Type: application/octet-stream');

// for example this will download any type of file

 header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

Does the HTTP Protocol support multiple content types in response headers?

You need to look at the definition of the header field:

http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.14.17

Content-Type = "Content-Type" ":" media-type

so it takes a single media-type, which is defined by the grammar you quoted above.

So the answer is: a single type/subtype, followed by optional parameters.

PHP header, Content type: image not allowing text

You need to make a separate PHP script which serves the image, then make an <img> tag that points to this script.

You can send information to the script using the querystring in the image URL.

outputting image content type using two functions

The final content you send to the client can have one content-type only. Either image or text. If you want both in a single page, (when image isn't written to file) you could write the image in base64 in the following way:

data:[<MIME-type>][;charset=<encoding>][;base64],<data>

In your case,

data:image/gif;base64,<base64_encoded_image>

See Data URI scheme


Something like the following will work: (Untested)

$img = fread(fopen($path, "r"), filesize($path));
$base64 = "data:image/gif;base64," . base64_encode($img);


Related Topics



Leave a reply



Submit