Change Mime Type of Output in PHP

change mime type of output in php

header('Content-type: application/xml');

More information available at the PHP documentation for header()

Use of exit instead of echo changes output mime type

My guess would be that code igniter uses output buffering to capture everything that's being echo'd, and set the mimetype and send the output after you've composed the full response body.

In the exit() case you completely termininate everything, so if code igniter does anything of this stuff after the controller logic, it will never get a chance to set the correct mime-type in the first place.

In general you should avoid calls to exit() unless you have a very specific reason for it.

Have Apache/PHP set MIME type based upon file extension for files being executed as PHP

It seems worth me noting that the only reason I ever ended up needing to ask this question was the wrong-headed approach I'd taken to having .js and .css files executed by PHP.

Don't modify your mime.types file to achieve this. Instead, in your Apache config (or in a .htaccess file), use invocations like this:

<Files *.js>
ForceType application/x-httpd-php
Header set Content-Type "application/javascript"
</Files>
<Files *.css>
ForceType application/x-httpd-php
Header set Content-Type "text/css"
</Files>

Changing the mime type of a non-php file?

If you have access to your Apache configuration (if you don't want to write a PHP script that will send the headers and the file content), maybe the AddType directive can help you :

The AddType directive maps the given
filename extensions onto the specified
content type. MIME-type is the MIME
type to use for filenames containing
extension. [...] This directive can be
used to add mappings not listed in the
MIME types file

The given example looks like this :

AddType image/gif .gif 

Getting mime type from file name in php

If you check the documentation, you can see that you are not doing anything wrong.
But if you do a bit more research:

https://stackoverflow.com/a/3664655/3784145

you can see that the mime type you get is correct, but the extension doesn't need to match with the mime type as explained here:

http://nl3.php.net/manual/en/function.mime-content-type.php#85879

I would therefore use the files suffix to determine the files mime type.
(as seen in the first example)



Related Topics



Leave a reply



Submit