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 given example looks like this :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
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
Moving Array Element to Top in PHP
Php: Move_Uploaded_File() Failed to Open Stream: No Such File or Directory
PHP Error: Cannot Use Object of Type Stdclass as Array (Array and Object Issues)
Best Way to Handle Email Parsing/Decoding in PHP
Zend Framework 2 Routing Subdomains to Module
How to Get Last Insert Id in Eloquent Orm Laravel
How to Use Xpath and Dom to Replace a Node/Element in PHP
Create a Zip File Using PHP Class Ziparchive Without Writing the File to Disk
Doctrine2 Findby Relationship Object Triggers String Conversion Error
PHP Class Not Found But It's Included
Exploding by Array of Delimiters
PHP Gd: How to Get Imagedata as Binary String
Zend Framework - Multiplate Navigation Blocks
Efficient Reloading Data/Pushing Data from Server to Client
Phpmyadmin Failed to Set Session Cookie. Maybe You Are Using Http Instead of Https