mime_content_type returning text/plain for css and js files only

时光毁灭记忆、已成空白 提交于 2019-12-04 05:40:16

问题


I'm encountering this weird issue with PHP's mime_content_type, it works fine, except for CSS and JavaScript files, which it returns text/plain for (PHP's default mime is set to do text/html).

Modifying the mime.types file seems to make no difference (its path on httpd.conf is correct).

Examples

<?php
  echo(mime_content_type('index.html')); // returns text/html
  echo(mime_content_type('default.png')); // returns image/png
  echo(mime_content_type('bootstrap.min.js')); // returns text/plain
  echo(mime_content_type('css/animate.css')); // returns text/plain
  echo(mime_content_type('css/style.css')); // returns text/x-asm
?>

The same exact files served on the same exact Apache installation gets correctly marked on their Content-Type headers, it's almost as if PHP's using its mime detection mechanism.

I'm using Wamp 3.0.6 64-bit and the built-in PHP 7.0.10 on Windows 10 64-bit build 15063.483 mod_mime and mode_mime_magic are enabled.


回答1:


The PHP doc is pretty explicit about that:

Returns the MIME content type for a file as determined by using information from the magic.mime file.

PHP and Apache don't use the same mime databases. Apache uses mime.types but PHP uses magic.mime (can't remember where it's located, /etc/ on a Unix system, I think)

No sure how to edit it on Windows but here are some tips for Linux: How to create a custom magic file database



来源:https://stackoverflow.com/questions/45179337/mime-content-type-returning-text-plain-for-css-and-js-files-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!