Set MIME type after PHP interpreting
By default ie. when: your mime.types does not contain (*):
application/x-httpd-php php
line and your httpd.conf does not contain (**):
PHPIniDir "C:/PHP/"
LoadModule php5_module "C:/PHP/php5apache2_4.dll"
(or similar depending on php's path and apache version)
.. any request to php file will return php's source code (not interpreted) and Apache will not set Content-Type so it is up to your browser how to interpret it (google Mime-type sniffing). Usually you'll just see your source code in the browser's viewport.
If your mime.types contains (*) and httpd.conf does not contain (**) it will tell Apache to serve your php files with Content-Type: application/x-httpd-php. Hovewer it will still be a source code and your browser will ask you whether to open/save requested resource.
In general if your httpd.conf contains (and loads php module this way):
PHPIniDir "C:/PHP/"
LoadModule php5_module "C:/PHP/php5apache2_4.dll"
(or similar depending on php's path and apache version) ... mime.types line:
application/x-httpd-php php
serves slightly different purpose than expected. It no longer provides MIME type for Content-Type: ... HTTP response. It tells Apache which file extensions should be sent to PHP interpreter. You can put to your mime.types the following line:
application/x-httpd-php xyz
and it will tell Apache to send contents of any file with xyz extension to PHP interpreter but your HTTP response will have Content-Type: text/html set and not as you might think application/x-httpd-php.
So you can accomplish what you want by adding this to your mime.types:
application/x-httpd-php phtml pwml php5 php4 php3 php2 php inc htm html
Of course you must have your PHP module loaded.
You may also want to read about mod_rewrite apache module.
Still even if you interpret files with *xyz (whatever) extension as PHP you may freely change content type using:
header('Content-Type: ....');
Related videos on Youtube
Comments
-
noober almost 2 years
When I use
AddType application/x-httpd-php .phtml .pwml .php5 .php4 .php3 .php2 .php .inc .htm .html
in httpd.conf, the files with the given extensions start being interpreted by PHP interpreter. But all of them also start having 'text/html' MIME type.
I know I can use
<?php header("Content-type: WHATEVER"); ?>
in the files themselves, but is it possible to assign a MIME type right after PHP interpreting for an extension, using the same httpd.conf?
What I want is a way to make any file type being interpreted by PHP, no matter what extension, and don't write header manually, since it always can be inferred from extension. In other words, I want to keep the MIME type a file would have if there wasn't assignment to PHP interpreter.
-
Hichem over 10 yearsso do you want to make a rule for all extensions to be set automaticly?
-
Hichem over 10 yearstry to edit
/etc/mime.types
this file countains all extension and their headers -
noober over 10 years@K3rnel31 It looks like the line from mime.types is overridden to text/html if you assign the extension to application/x-httpd-php. At least, I explicitly set the custom MIME type in this very file and the file is still text/html.
-
Hichem over 10 years@noober the browser can not identify the type of application/x-httpd-php it converts it to text/html
-
noober over 10 years@K3rnel31 Oh, I see now. I used to think the type is changed to text/html on server side. OK, then how to make PHP interpreter to process the files, but not setting its type to application/x-httpd-php?
-
noober over 10 years@Artur I don't understand about mod rewrite. Could you please add some info?
-
Hichem over 10 yearsmmm as i see you want that you request application/x-httpd-php on your server-side and process it on php ?
-
noober over 10 years@K3rnel31 I want, for instance, .txt remains text/plain (as it is in
mime.types
) without <?php header("Content-type: text/plain"); ?>, but the same time being processed by PHP. -
Hichem over 10 yearsthis is by defaults on your
mime.types
file open it and you will seetext/plain txt
-
-
Artur over 10 years@K3rnel31: Do you think that if some part of my answer is within comments I do not know what I am talking about here? It takes some time to write it as it is longer than comments.
-
noober over 10 yearsThat's very interesting, but how get rid of header('Content-Type: ....');? This is the question. Even if there is no lines of <?php ?> in a file, I should add this line! Then I cannot just copy income files! I have to process them manually, adding header at the top! This is what my question is about.
-
Artur over 10 years@noober: You do not have to get rid of it - it is not necessary. What I meant in last sentence is that by changing content type from php you may force browsers to interpret what you output as an image (whatever - even pdf): header('Content-Type: image/png'); echo file_get_contents(png_file);