Css mime type return as text/plain rather than text/css

mime-types

I am using this code to get the mime type from a given file

$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime  = $finfo->buffer(file_get_contents($file));

If I feed it a PHP file then I get

text/x-php

but if I feed it a CSS file I get

text/plain

I have been trying to solve this issue to no avail, I have AddType in my Apache to allow for CSS file types.

Anyone have any suggestions?

Best Answer

You need to tell Apache to serve files as text/css whenever their extension is .css. The only sane way for your server to differentiate between a style sheet and any other type of text file, is by its file name.

You can do so by editing Apache's MIME type configuration file, which is named mime.types ( it lives in a different folder depending on your distro, maybe try /etc/apache2 ) to make the following association:

text/css                    css

Alternately, if you don't have admin access, you can create an .htaccess file in your own web root. More info on both methods here

Hope this helps!

Upon rereading our question, maybe I misunderstood you, and you have already done the steps above?

If so, could you check what your OS is reading the mimetype as? There are appropriiate commands to check mimetype, over here:

Also, it seems like it would be pragmatic to add a subsequent check to see if the file extension ends with .css. Is there a reason why this won't work?